Table of contents | |
Introduction | |
What is a Function Pointer? | |
Declaring a Function Pointer | |
Using Function Pointers | |
Conclusion |
Function pointers are a powerful feature of C++ programming language. They allow us to store the address of a function in a pointer variable and then use that pointer to call the function. This article aims to provide an easy-to-understand explanation of function pointers in C++.
A function pointer is a variable that stores the address of a function. In C++, functions are first-class citizens and can be treated like any other data type. Therefore, a function pointer is just like a regular pointer, but instead of pointing to an object, it points to a function.
To declare a function pointer, we use the following syntax:
return_type (*pointer_name)(argument_type1, argument_type2, ..., argument_typeN);
Here is an example of declaring a function pointer:
int (*p)(int, int);
This declares a function pointer named 'p' that can point to a function that takes two 'int' arguments and returns an 'int'.
Once we have declared a function pointer, we can assign a function to it. Here is an example of assigning a function to a function pointer:
int add(int a, int b) {
return a + b;
}
int (*p)(int, int) = add;
In this example, we have assigned the 'add' function to the 'p' pointer. Now we can call the 'add' function using the 'p' pointer like this:
int result = p(3, 4);
This will call the 'add' function with the arguments '3' and '4', and store the 'result' in the result variable.
We can use function pointers in many ways. Here are some examples:
1. Passing Function Pointers as Arguments:
We can pass a function pointer as an argument to another function. Here is an example:
void print_result(int (*operation)(int, int), int a, int b) {
int result = operation(a, b);
std::cout << "Result: " << result << std::endl;
}
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
int (*p)(int, int) = add;
print_result(p, 3, 4);
p = multiply;
print_result(p, 3, 4);
return 0;
}
In this example, we have defined a function 'print_result' that takes a function pointer as its first argument. We then pass the 'add' function and the 'multiply' function as arguments to 'print_result' using the 'p' pointer.
2. Storing Function Pointers in Arrays:
We can store function pointers in arrays. Here is an example:
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
int (*operations[])(int, int) = {add, multiply};
int result = operations[0](3, 4);
std::cout << "Result: " << result << std::endl;
result = operations[1](3, 4);
std::cout << "Result: " << result << std::endl;
return 0;
}
In this example, we have defined an array 'operations' that stores two function pointers. We then use the function pointers to call the 'add' function and the 'multiply' function.
1. Write a program that uses a function pointer to find the maximum of two integers.
#include <iostream>
int max(int a, int b) {
return a > b ? a : b;
}
int main() {
int (*p)(int, int) = max;
int result = p(3, 4);
std::cout << "Result: " << result << std::endl;
return 0;
}
Result: 4
2. Write a program that uses a function pointer to calculate the sum of an array of integers.
#include <iostream>
int sum(int* arr, int size) {
int result = 0;
for (int i = 0; i < size; i++) {
result += arr[i];
}
return result;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int (*p)(int*, int) = sum;
int result = p(arr, size);
std::cout << "Result: " << result << std::endl;
return 0;
}
Result: 15
Function pointers are a powerful feature of C++ that allows us to store the address of a function in a pointer variable and then use that pointer to call the function. We can use function pointers in many ways, such as passing them as arguments to other functions, storing them in arrays, and more. With these examples, you should be able to start using function pointers in your own programs.
70 videos|45 docs|15 tests
|
|
Explore Courses for Software Development exam
|