Function Pointer in C++

Last Updated : 19 Jan, 2026

In C++, pointers store memory addresses. While pointers are commonly used with variables, they can also store the address of a function. A function pointer allows a program to call a function indirectly or pass a function as an argument, enabling flexible and dynamic behavior.

Address of a Function

Every function resides in memory and therefore has an address. The name of a function can be used to find the address of the function. We can get the address of a function by just writing the function's name without parentheses in the function.

Example:

multiply; // Represents the address of the function multiply

Function Pointer in C++

A function pointer is a pointer variable that stores the address of a function with a specific return type and parameter list.

Key Uses:

  • Calling functions indirectly
  • Passing functions as arguments
  • Implementing callbacks
  • Creating function tables
Function Pointer in C++
Function Pointer in C++

Syntax

return_type (*FuncPtr) (parameter type, ....);

Example:

int (*func)(int, int);

Referencing and Dereferencing a Function Pointer

1. Referencing: Assigning a function’s address to the function pointer.

Syntax:

FuncPtr= function_name;

2. Dereferencing: Invoking the function using the pointer. The dereference operator * is optional during function calls.

Syntax:

FuncPtr(10, 20); // Preferred
(*FuncPtr)(10, 20); // Also valid

Calling a Function Using a Function Pointer

In this example, we see how we point a pointer to a function and call it using that pointer:

C++
#include <iostream>
using namespace std;

int multiply(int a, int b) {
    return a * b;
}

int main() {
    int (*func)(int, int);

    // func points to multiply
    func = multiply;

    int prod = func(15, 2);
    cout << "The value of the product is: " << prod << endl;

    return 0;
}

Output
The value of the product is: 30

Explanation:

  • func stores the address of multiply
  • Calling func(15, 2) executes multiply
  • The call is equivalent to a direct function call

Passing a Function Pointer as a Parameter

Instead of passing a function’s return value, we can pass the function itself. This allows the receiving function to decide when to execute it.

C++
#include <iostream>
using namespace std;

const int a = 15;
const int b = 2;

// Function for multiplication
int multiply() {
    return a * b;
}

// Function accepting a function pointer
void print(int (*funcptr)()) {
    cout << "The value of the product is: " << funcptr() << endl;
}

int main() {
    print(multiply);
    return 0;
}

Output
The value of the product is: 30

Explanation:

  • multiply is passed as a function pointer to print
  • funcptr() invokes multiply inside print
  • This technique is widely used in callbacks and APIs
Comment