As we know, pointers are used to point to variables; similarly, function pointers are used to point to functions. They are primarily used to store the address of a function. We can use function pointers to call functions or pass function pointers as parameters to another function.
Function pointers are mainly useful in scenarios such as event-driven applications, callback functions, and storing functions in arrays.
What is the Address of a Function?
Computers can only understand low-level languages, i.e., binary form. The programs we write in C++ are always high-level languages, so to convert the program into binary form, we use a compiler. A compiler is a program that converts source code into an executable file. This executable file is stored in RAM. The CPU starts executing from the main() method and reads a copy from RAM, not the original file.
All functions and machine code instructions are data. This data consists of a bunch of bytes, and all these bytes have some addresses in RAM. A function pointer contains the RAM address of the first instruction of the function.
👇Click to Claim👇
👉C Language Knowledge Resource Collection
Declaration Syntax
The declaration syntax for a function pointer is as follows:
int (*FuncPtr) (int, int); *
The above syntax is the declaration of a function. Since functions are not as simple as variables, but C++ is type-safe, function pointers have return types and parameter lists. In the above syntax, we first provide the return type, then the name of the pointer, which is FuncPtr, enclosed in parentheses and prefixed with the pointer symbol (*). After that, we provide the parameter list (int, int). The above function pointer can point to any function that accepts two integer parameters and returns an integer type value.
Address of a Function
We can easily obtain the address of a function. We just need to mention the name of the function without calling it.
Let’s illustrate this with an example.
#include <iostream>
using namespace std;
int main(){
cout << "The address of the main() function is:" << &main << endl;
return 0;
}
In the above program, we display the address of the main() function. To print the address of the main() function, we just need to mention the function’s name, without parentheses or parameters. Thus, merely mentioning the function’s name, without parentheses or parameters, indicates the address of the function.
We can also print the address of the function using another method, i.e., &main.
Indirect Function Call
We can call a function through a function pointer by simply using the name of the function pointer. The syntax for calling a function through a function pointer is similar to the syntax for calling a function normally.
Let’s understand this situation with an example.
#include <iostream>
using namespace std;
int add(int a, int b){
return a + b;
}
int main(){
int (*funcptr)(int, int); // Function pointer declaration
funcptr = add; // funcptr points to add function
int sum = funcptr(5, 5);
cout << "The value of sum is:" << sum << endl;
return 0;
}
In the above program, we declare a function pointer int (*funcptr)(int, int), and then store the address of the add() function in funcptr. This means funcptr contains the address of the add() function. Now, we can use funcptr to call the add() function. The statement funcptr(5, 5) calls the add() function, and the result of the add() function is stored in the sum variable.
Output:
The value of sum is: 10 Another example of function pointers.
#include <iostream>
using namespace std;
void printname(char *name){
cout << "Name is: " << name << endl;
}
int main(){
char s[20]; // Array declaration
void (*ptr)(char *); // Function pointer declaration
ptr = printname; // Store the address of printname in ptr
cout << "Enter a person's name:" << endl;
cin >> s;
cout << s;
ptr(s); // Call printname() function
return 0;
}
In the above program, we define a function printname() that takes a char pointer as a parameter. We declare a function pointer void (*ptr)(char *). The statement ptr = printname indicates that we assign the address of the printname() function to ptr. Now, we can call the printname() function using the statement ptr(s).
Output:
Passing Function Pointers as Parameters
Function pointers can be passed as parameters to another function.
Let’s understand this with an example.
#include <iostream>
using namespace std;
void func1(){
cout << "func1 was called";
}
void func2(void (*funcptr)()){
funcptr();
}
int main(){
func2(func1);
return 0;
}
In the above code, the func2() function accepts a function pointer as a parameter. The main() method calls the func2() function, passing the address of func1() as a parameter. This way, the func2() function indirectly calls func1().
Output:
Popular Recommendations
-
CLion Tutorial – Modifying the Directory Used by CLion
-
C Language Algorithm – “Plus One” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of References vs Pointers in C++