A function is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a function so that instead of writing the same code again and again for different inputs, we can call this function.
In simple terms, a function is a block of code that runs only when it is called.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; // Following function that takes two parameters 'x' and 'y' // as input and returns max of two input numbers int max(int x, int y) { if (x > y) return x; else return y; } // main function that doesn't receive any parameter and // returns integer int main() { int a = 10, b = 20; // Calling above function to find max of 'a' and 'b' int m = max(a, b); cout << "m is " << m; return 0; } |
Output:
1 | m is 20 |
Function Definition
Pass by value is used where the value of x is not modified using the function fun().
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; void fun(int x) { // definition of // function x = 30; } int main() { int x = 20; fun(x); cout << "x = " << x; return 0; } |
Output:
1 | x = 20 |
Function Declaration
A function declaration tells the compiler about the number of parameters, data types of parameters, and returns type of function. Writing parameter names in the function declaration is optional but it is necessary to put them in the definition. Below is an example of function declarations. (parameter names are not present in the below declarations).
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | int max(int, int); // A function that takes an int // pointer and an int variable // as parameters and returns // a pointer of type int int* swap(int*, int); // A function that takes // a char as parameter and // returns a reference variable char* call(char b); // A function that takes a // char and an int as parameters // and returns an integer int fun(char, int); |
Calling a Function
In the above program, we have declared a function named greet(). To use the greet() function, we need to call it.
1 2 3 4 | int main() { // calling a function greet(); } |
Parameters and Arguments
Information can be passed to functions as a parameter. Parameters act as variables inside the function.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
Syntax:
{
// code to be executed
}
1 2 3 4 5 6 7 8 9 | void myFunction(string fname) { cout << fname << " Refsnes\n"; } int main() { myFunction("Liam"); myFunction("Jenny"); myFunction("Anja"); return 0; } |
Output:
1 2 3 | Liam Refsnes Jenny Refsnes Anja Refsnes |
Call Type | Description |
---|---|
Value | This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. |
Pointer | This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. |
Reference | This method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. |