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:
- The function
add
takes two integers as input and returns their sum. - The function pointer
operation
is declared to point to functions with the signatureint(int, int)
. - The address of the
add
function is assigned tooperation
. - The function pointer
operation
is used to call theadd
function, resulting in the sum of10
and20
.
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:
- The
compute
function takes two integers and a function pointer as arguments. - The function pointer
operation
is used to call either theadd
orsubtract
function, depending on what is passed tocompute
. - In
main
,compute
is called twice: once with theadd
function pointer and once with thesubtract
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:
- An array of function pointers
operations
is declared to hold pointers to the functionsadd
,subtract
, andmultiply
. - Each index in the array corresponds to one of the functions.
- The array elements are used to dynamically call the respective functions, passing the arguments
x
andy
.
Key Points about Function Pointers
- Function pointers store the address of a function and allow it to be called indirectly.
- They are commonly used in callback mechanisms, dynamic function calls, and event-driven programming.
- Function pointers can be passed as arguments to functions, returned from functions, or stored in arrays.
- The function signature (return type and parameter list) must match the declaration of the function pointer.
- Using function pointers can make your code more flexible and modular.