Function Pointers in C++

Function pointers in C++ are pointers that store the address of a function. They enable functions to be passed as arguments, returned from other functions, and stored in data structures.

Function pointers are commonly used in scenarios like callback functions, event-driven programming, and dynamic function calls.


Syntax

</>
Copy
return_type (*pointer_name)(parameter_list);
return_type
The return type of the function the pointer will point to.
pointer_name
The name of the function pointer.
parameter_list
The list of parameters (if any) that the function accepts.

Examples

Example 1: Declaring and Using a Function Pointer

This example demonstrates the basic declaration and usage of a function pointer.

</>
Copy
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    int (*operation)(int, int); // Declare a function pointer
    operation = &add;          // Assign the address of the add function

    cout << "Result: " << operation(10, 20) << endl; // Call the function using the pointer
    return 0;
}

Output:

Result: 30

Explanation:

  1. The function add takes two integers as input and returns their sum.
  2. The function pointer operation is declared to point to functions with the signature int(int, int).
  3. The address of the add function is assigned to operation.
  4. The function pointer operation is used to call the add function, resulting in the sum of 10 and 20.

Example 2: Passing a Function Pointer as a Parameter

This example demonstrates how a function pointer can be passed as a parameter to another function.

</>
Copy
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int compute(int x, int y, int (*operation)(int, int)) {
    return operation(x, y); // Call the function through the pointer
}

int main() {
    cout << "Addition: " << compute(10, 5, add) << endl;
    cout << "Subtraction: " << compute(10, 5, subtract) << endl;
    return 0;
}

Output:

Addition: 15
Subtraction: 5

Explanation:

  1. The compute function takes two integers and a function pointer as arguments.
  2. The function pointer operation is used to call either the add or subtract function, depending on what is passed to compute.
  3. In main, compute is called twice: once with the add function pointer and once with the subtract function pointer.

Example 3: Array of Function Pointers

This example demonstrates how to use an array of function pointers to store and call multiple functions dynamically.

</>
Copy
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

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

int main() {
    // Array of function pointers
    int (*operations[3])(int, int) = {add, subtract, multiply};

    int x = 10, y = 5;
    cout << "Addition: " << operations[0](x, y) << endl;
    cout << "Subtraction: " << operations[1](x, y) << endl;
    cout << "Multiplication: " << operations[2](x, y) << endl;

    return 0;
}

Output:

Addition: 15
Subtraction: 5
Multiplication: 50

Explanation:

  1. An array of function pointers operations is declared to hold pointers to the functions add, subtract, and multiply.
  2. Each index in the array corresponds to one of the functions.
  3. The array elements are used to dynamically call the respective functions, passing the arguments x and y.

Key Points about Function Pointers

  1. Function pointers store the address of a function and allow it to be called indirectly.
  2. They are commonly used in callback mechanisms, dynamic function calls, and event-driven programming.
  3. Function pointers can be passed as arguments to functions, returned from functions, or stored in arrays.
  4. The function signature (return type and parameter list) must match the declaration of the function pointer.
  5. Using function pointers can make your code more flexible and modular.