Definition:
A function is that part of the code, which contains a certain set of statements, which yield a certain result, on giving different inputs. A function is something, which gives us results when it is called. There are various types of functions and can be user-defined as well. Functions can be simply considered as a machine, which takes some input, and gives us some output according to the function definition.
Why do we need a function?
Sometimes, we have to do a certain calculation many times in a code, and if we write code for that calculation, again and again, then it would be a waste of time, and also will make our code a bit messy.
So instead of all this, we write a function, and whenever we need to calculate the same thing, we just need to call the function, and hence, we don’t need to write the code again and again, and also it will make our code more modular( dividing the code into separate subcodes for better readability and understandability).
And also if there, is a very big code file, then instead of changing the same calculation, we just need to change the function definition.
How to write a function in C++?
There is a general pattern, we need to follow, every time we write a function
Return_type function_name(arg1 , arg2 , …){ Function statements return statement }
- Return_type: the type of value, the function is returning. This can be int, bool, float, double, string, or any data type, void (when we don’t want our function to return anything, else it could be used to display some information ).
- Function_name: the name of the function, whatever we can give.
- Arguments: if our function needs some arguments to calculate, then we need to pass the arguments. we can pass them in any variable type, and can pass as many arguments as per our needs.
- Function statements: what the function is doing
- Return statement: what the function should return after calculating.
Example: let’s take an example to understand it better.
This is a simple function to calculate the product of two numbers.
Code:
C++ Code
int product(int x, int y) { //function declaration , and passing parameters
return x *y; //a simple function returning the product of two numbers .
}
int main() {
int a;
cin >> a;
int b;
cin >> b;
int ans = product(a, b); //function call we need to create the variable
//because if the function is returning something , then we need to have some
//variable to store the returned value.
}
Types of arguments
We can pass arguments in the function in two ways –
- Static arguments: the parameters passed to a function, while a function call. For example – a and b are static arguments in the above code.
- Formal arguments: the parameters received by the function are formal arguments. For example – x and y are formal arguments in the above code.
How to pass arguments in the function
There are two ways to pass arguments in a function –
- Pass by value: In this, the value of the actual parameters is copied to the formal parameters and the actual and formal parameters point to different memory locations. So, even if we change something in the parameters, there is no change in the actual value.
- Pass by reference: In this, no copy is created, and the actual and formal parameters point to the same memory locations. so if a change is made in formal parameters then, actual parameters will also change.
Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article