Introduction to C++ | Function Pointers
C++ Function Pointer
A function pointer in C++ is a pointer that points to a function instead of a variable. It allows you to store the address of a function and call that function through the pointer. Function pointers are often used to implement callback functions, pass functions as arguments to other functions, or store a set of functions that can be executed dynamically.
What is a Function Pointer?
A function pointer can store the address of a function, and later, you can use it to invoke the function. It is a powerful feature of C++ and allows for more flexible and dynamic behavior in your programs.
Syntax:
Syntax of Function Pointer
returnType (*pointerName)(parameterTypes);
Example of Function Pointer
Let’s look at an example where we define a function and use a function pointer to call it:
Code Example: Function Pointer
#include <iostream>
using namespace std;
// A simple function to be called via pointer
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer
int (*funcPtr)(int, int);
// Assign the address of the 'add' function to the pointer
funcPtr = add;
// Call the function through the pointer
int result = funcPtr(10, 20);
cout << "Result of addition: " << result << endl;
return 0;
}
Output
Using Function Pointers as Function Arguments
Function pointers are useful when you need to pass a function as an argument to another function. This allows dynamic behavior based on the function passed at runtime.
Example of passing a function pointer as an argument:
Code Example: Function Pointer as Argument
#include <iostream>
using namespace std;
// A simple function to add two numbers
int add(int a, int b) {
return a + b;
}
// A simple function to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
// Function that accepts a function pointer as an argument
int operate(int x, int y, int (*operation)(int, int)) {
return operation(x, y); // Call the function pointed to by operation
}
int main() {
int x = 10, y = 20;
// Pass the function pointer for 'add'
cout << "Addition result: " << operate(x, y, add) << endl;
// Pass the function pointer for 'multiply'
cout << "Multiplication result: " << operate(x, y, multiply) << endl;
return 0;
}
Output
Multiplication result: 200
Array of Function Pointers
It is also possible to have an array of function pointers. This is useful when you want to dynamically select and call a set of functions from an array based on some condition or user input.
Example of an array of function pointers:
Code Example: Array of Function Pointers
#include <iostream>
using namespace std;
// Function to add
int add(int a, int b) {
return a + b;
}
// Function to multiply
int multiply(int a, int b) {
return a * b;
}
int main() {
// Declare an array of function pointers
int (*operations[2])(int, int) = { add, multiply };
int x = 10, y = 20;
// Call each function using the array of function pointers
cout << "Addition result: " << operations[0](x, y) << endl; // Call 'add'
cout << "Multiplication result: " << operations[1](x, y) << endl; // Call 'multiply'
return 0;
}
Output
Multiplication result: 200
Pro Tip:
💡 Pro Tip
Function pointers are powerful but can be tricky to use. Ensure that the function signatures match exactly when assigning functions to function pointers. Additionally, when passing function pointers to functions, make sure that the functions accept the correct parameters and return types.