Introduction:
Function pointers in c++
Pointers:
In C++, Pointers are the variables that consist of addresses of other variables. A pointer not only stores the address of a single variable, but it can also store the address of cells of an array, string, vector, etc..
Pointer variables contain the addresses of other standard variables.”*” indicates that the following variable is a pointer.
Dereferencing a pointer:
We use a * symbol to access the value of a variable that a pointer is pointing to.
Ex:
int * p;
int var=10;
p=&var;
cout<<*p<<endl;
Functions:
A function is a block of code or set of instructions that are used to perform a specific task.
C++ function declaration:
Syntax:
return_type function_name(data_type parameter1,data_type parameter 2,...){
//body of the function
}
Ex:
int add(int x,int y){
return x+y;
}
function pointers in c++
Here add() is the name of the function, return type is int and function contains two parameters x and y.
Calling a function
In order to execute the function,we need to call the function.This can be done in the following way.
Ex:
int main() {
// calling a function
int a=10,b=20;
add(a,b);
}
Program to add two numbers
// program to add two numbers using a function
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum;
// calling the function and storing
// the returned value in sum
int a=10,b=20;
int sum = add(a,b);
cout<<”sum of a and b is ”<<sum<<endl;
return 0;
}
function pointers in c++
Here , the output is sum of a and b is 30.
We have passed the values of a and b while calling the function.The returned value is stored in the variable sum.
Passing Pointers to functions
This means declaring the function parameters as pointer variables and passing the addresses while calling the function. In order to manipulate the values of any variable in the function we have to pass the address of that variable in the function.
Let us consider an example of a function that swaps two numbers.
#include <iostream>
using namespace std;
void swap( int *a, int *b ) {
int t;
t = *a;
*a = *b;
*b = t;
}
int main(){
int num1, num2;
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2; swap( &num1, &num2);
cout << "First number = " << num1 << endl;
cout << "Second number = " << num2 << endl;
return 0;
}
function pointers in c++
void swap( int *a, int *b ) – It means our function ‘swap’ is taking two pointers as argument. So, while calling this function, we will have to pass the address of two integers ( call by reference ).
int t; t = *a; We took any integer t and gave it a value ‘*a’.
*a = *b – Now, *a is *b. This means that now the values of *a and *b will be equal to that of *b.
*b = t; – Since ‘t’ has an initial value of ‘*a’, therefore, ‘*b’ will also contain that initial value of ‘*a’. Thus, we have interchanged the values of the two variables.
Since we have done this swapping with pointers we have targeted on address, so, this interchanged value will also reflect outside the function and the values of ‘num1’ and ‘num2’ will also get interchanged.
In the swapping example also, we used call by reference in which we passed the address of num1 and num2 as the arguments to the function. The function parameters ‘a’ and ‘b’ point to the address of num1 and num2 respectively. So, any change in the parameters ‘a’ and ‘b’ changes the value of num1 and num2 also.
Here, in this article, We have try to explain Function Pointer in C++ Language with examples. we hope you enjoy this Function Pointer in C++ with examples article.