How to create and use function pointers in C++

Pointers in C++ are used to store the memory addressesIt is the location on the computer’s memory where a variable is stored. of variables. Similarly, function pointers are used to store the addresses of functions rather than variables. Function pointers are used for the following reasons:

  • They allow us to execute different functions on runtimeThe phase of a computer program in which the program is run or executed..
  • They are used for the callback mechanismPassing the function pointer to a function or library to call a specific function..

Syntax

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);
//or
ReturnType (*pointerName)(Parameter);
Syntax to declare a function pointer
  • 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 declaration
float mult(float para_1, int para_2);
//Function pointer declaration
float (*multPointer)(float, int);
Declaring a function pointer example

Assigning a function

After declaring the function pointer, we can assign it to the function.

multPointer = mult;
Assigning a function to the function pointer

Calling a function

We can call the mult() function using the multPointer function pointer.

int answer = multPointer(5.5, 3);
Calling the mult() function using a function pointer

Examples

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 pointer
float (*func_pointer)(float, float);
// Assign 'add' function to the pointer
func_pointer = addition;
cout << "Result of addition: " << func_pointer(1000.35, 5.45) << endl;
// Assign 'subtract' function to the pointer
func_pointer = subtraction;
cout << "Result of subtraction: " << func_pointer(456.7, 23.345) << endl;
return 0;
}

Explanation

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() function
float multiplication(float var_1, float var_2)
{
return var_1 * var_2;
}
//define another function which takes function as a parameter
float 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 parameter
result = func_in_func(10.5, 20.5, multiplication);
cout<<"Multiplication Result: "<<result;
return 0;
}

Explanation

  • 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.

Conclusion

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

Copyright ©2025 Educative, Inc. All rights reserved