A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument to be passed while calling a function then it is called actual arguments. There are two ways to call a function:
- call by value
- call by reference
In a first way, we pass the actual values as an actual argument and those values are copied to the function and the function does some task with them. And the second way to call a function can be processed in two ways:
- by passing the references
- by passing the pointers
In C++ programming, just like we pass values of the variable as parameters in the function, we can also pass addresses as an argument in functions.
When we pass pointers in the function that means we pass the address of the variable instead of the value.
Let us understand it through a syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // & before the parameter is used for address void func(int &num) { // code } int main() { int number = 10; // pass by reference func(number); return 0; } |
As you can see that the func()
function takes a reference of the number variable that is the address of the number. And &
is used to access the address.
Example: Passing Pointer to Function by reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; // swap function void swapFunc(int *num1, int *num2) { int temp; temp = *num1; *num1= *num2; *num2= temp; } int main() { int a = 4, b = 5; cout << "Values of a and b, before swapping" << endl; cout << "a: " << a << endl; cout << "b: " << b << endl; // calling swap function by passing a reference swapFunc(&a, &b); cout << "\nValues of a and b, after swapping" << endl; cout << "a: " << a << endl; cout << "b: " << b << endl; return 0; } |
Output: After execution of the above code, you will get the following result.
1 2 3 4 5 6 7 | Values of a and b, before swapping a: 4 b: 5 Values of a and b, after swapping a: 5 b: 4 |
In the above program, we use *
in the swapFunc()
function with passing reference to swap the number because we are dealing with the address so it is necessary to put *
while using those variables.
Return Pointer from Functions
As we know, in C++ that array can be returned from the function, similarly, a pointer can also be returned from a function. For this, we need to declare the returning function as the pointer in the following way.
1 2 3 4 5 | //return int type int * functionName() { //statements } |
Also, note that it is considered bad practice to return the address of a local variable from a function, so define the local variable as a static variable.
Now let us see an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> using namespace std; //user defined funtion int *getArray() { static int r[5] = { 0, 10, 20, 30, 40 }; for (int i = 0; i < 5; ++i) { r[i] += 5; cout << r[i] << endl; } return r; } //main function int main() { int *ptr; ptr = getArray(); for (int i = 0; i < 5; i++) { cout << "*(p + " << i << ") : "; cout << *(ptr + i) << endl; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | 5 15 25 35 45 *(p + 0) : 5 *(p + 1) : 15 *(p + 2) : 25 *(p + 3) : 35 *(p + 4) : 45 |