In C++ we can also have Pointers to Functions. These pointers behave in different manner than that of other pointers. A Function Pointer is a type of variable which stores the address of a function which can be called upon later through Function Pointer. It is useful because functions confine behavior. Instantly if you need a particular behavior any time and every time such as writing a long code, drawing circle , drawing line so instead of doing this lengthy work you just have to call the function.Which make you work easy and also saves your time. Here the method used is called as the Passing by value due to passing of actual value .
Syntax:
Data_type(*pointer Name)(parameters);
For Example:
void fn 1(int number Val) { // code } // function that takes reference as parameter// notice the & before the parameter void fn 2(int&number Ref) { // code } int main() { int number = 10; // pass by value fn 1(number); // pass by reference fn 2(number); return 0; }
Here you can see that & in void fn 2(int &number Ref)
. This clearly shows that we used
the address of the variable as our parameter.
So, when we call the fn 2()
function in main()
by passing the variable number as an
argument, we are actually passing the address of number variable instead of the value 10.
Example 1: Passing by Reference without Pointers
#include <iostream> using namespace std; // function definition to swap values void swap(int &n1, int &n2) { int temporary; temporary = n1; n1 = n2; n2 = temporary; } int main() { // initialize variables int p = 5, q = 10; cout << "Before swapping" << endl; cout << "p = " << p << endl; cout << "q = " << q << endl; // call function to swap numbers swap(p, q); cout << "\n After swapping" << endl; cout << "p = " << p << endl; cout << "q = " << q << endl; return 0; }
Output:
Before swapping p = 5 q = 10 After swapping p = 10 q = 5
In this above program, we have passed the variables p
and q
to the swap()
function.
void swap(int &n1, int &n2)
In the above code we have used & in order to tell that that the function will accept
addresses as its parameters not the value .So, the compiler can identify that actual values
are not passed in place of actual value, the reference of the variables is passed to function
parameters. If we look at the swap()
function, the function parameters n1
and n2
seems
pointing to the same value as the variables p
and q
. Hence the numbers get swapped.
Example 2: Passing by reference using Pointers
#include <iostream>using namespace std; // function prototype with pointer as parametersvoid swap(int*, int*); int main() { int p = 5, q = 10; cout << "Before swapping" << endl; cout << "p = " << p << endl; cout << "q = " << q << endl; // call function by passing variable addresses swap(&p, &q); cout << "\n After swapping" << endl; cout << "p = " << p << endl; cout << "q = " << q << endl; return 0; } // function definition to swap numbers void swap(int* n1, int* n2) { int temporary; temporary = *n1; *n1 = *n2; *n2 = temporary; }
Output:
Before swapping p = 5 q = 10 After swapping p = 10 q = 5
In the above example the result is same as that of previous one.
swap(&p, &q);
In the above example the address of the variable is passed during the function call not the variable.Since the address is passed in place of value, a dereference operator * must be used to access the value stored in that address.
temporary = *n1; *n1 = *n2; *n2 = temporary;
*n1
and *n2
gives the value stored at address n1
and n2
.
Since n1
and n2
contain the addresses of p
and q
, anything is done to *n1
and *n2
will
change the actual values of p and q.Hence, when we print the values of p
and q
in
the main()
function, the values are changed.