Pointers in C++ are used to store the
The detailed syntax to define and use a function pointer is discussed in this section. We have the general syntax to declare a function pointer below:
typedef ReturnType (*pointerName)(Parameter);//orReturnType (*pointerName)(Parameter);
ReturnType
: It is the expected type of value that the function will return.
*pointerName
: It is the name of the function pointer and it can be anything. The *
symbol indicates that it is a pointer.
Parameter
: The parameter(s) to be passed to that function.
Let’s consider the function pointer syntax with an example. Suppose we have the mult()
function, which takes two integers as parameters and returns an integer as a result.
//Function declarationfloat mult(float para_1, int para_2);//Function pointer declarationfloat (*multPointer)(float, int);
After declaring the function pointer, we can assign it to the function.
multPointer = mult;
We can call the mult()
function using the multPointer
function pointer.
int answer = multPointer(5.5, 3);
Below is an example of how function pointers work across different functions:
#include <iostream>using namespace std;float addition(float var_1, float var_2){return var_1 + var_2;}float subtraction(float var_1, float var_2){return var_1 - var_2;}int main() {// Declare function pointerfloat (*func_pointer)(float, float);// Assign 'add' function to the pointerfunc_pointer = addition;cout << "Result of addition: " << func_pointer(1000.35, 5.45) << endl;// Assign 'subtract' function to the pointerfunc_pointer = subtraction;cout << "Result of subtraction: " << func_pointer(456.7, 23.345) << endl;return 0;}
Let’s discuss the above code in detail.
Lines 4–12: We define two simple functions, which take two float
variables and return their result.
addition()
: It takes two variables and returns their addition.
subtraction()
: It takes two variables and returns their subtraction.
Line 16: We declare a function pointer.
Lines 19–20: We assign the addition()
function to the function pointer and print its result.
Lines 23–24: We assign the subtraction()
function to the function pointer and print its result.
Below is an example of how function pointers work to pass a function as a parameter to another function:
#include <iostream>using namespace std;//define multiplication() functionfloat multiplication(float var_1, float var_2){return var_1 * var_2;}//define another function which takes function as a parameterfloat func_in_func(float para_1, float para_2, float (*multPointer)(float, float)){return multPointer(para_1, para_2);}int main(){float result;//calling func_in_func() function and passing multiplication() function as parameterresult = func_in_func(10.5, 20.5, multiplication);cout<<"Multiplication Result: "<<result;return 0;}
Lines 5–8: We define the multiplication()
function, which takes two float
values as a parameter and returns their result.
Lines 11–14: We define the func_in_func()
function, which takes two float
values, a function as a parameter, and calls the passed function.
In the func_in_func()
function definition, the third parameter *multPointer
is the function pointer, which is declared as the syntax mentioned above.
Line 20: We call the func_in_func()
function and pass two float
values and the multiplication()
function as a parameter.
Line 21: We print the output.
In this Answer, we discussed the function pointer and its usage in two different ways. We can use a function pointer to execute a function on runtime or pass a function to another function in C++.
Free Resources