Syntax
The syntax for declaring a function pointer
int *funcPtr(int, int)
Computers can only understand the binary form, which is a low-level language. Since the C++ code we write is always written in a high-level language, a compiler is used to translate it into binary code. A compiler is a program that mainly transforms the source code into executable code. This executable file is kept in memory (RAM). The main() method is where the CPU begins execution. It reads the copy that is currently in RAM but not in the original file.
All of the machine code instructions as well as functions are data. This data consists of a number of bytes, each byte has a RAM address. The initial instruction of a function is located at the RAM address in the function pointer.
We can quickly obtain the address of a function. For that we do not have to call the function, we only need to specify its name.
Let us understand this concept with a simple example:
#include <iostream>
using namespace std;
int main() {
cout << "Address of a main() function is: " << &main << endl;
return 0;
}
Output:
Address of a main() function is: 1
Working of the program
We will be able to call the function using a function pointer simply by supplying the function pointer's name.
The syntax used to call a function through a function pointer would be the same as when we call a function directly or normally.
Example: Using a function pointer let us write a C++ program in order to print the sum.
#include <iostream>
using namespace std;
int sum(int a, int b) {
return a + b;
}
int main() {
int(*funcPtr)(int,int); // function pointer is declared
funcPtr = sum; // funcPtr is pointing towards the sum function
int sum = funcPtr (2, 4);
cout << "Then the value of sum is: " << sum << endl;
return 0;
}
Output:
Then the value of sum is: 6
The function pointer is declared in the above program as int (*funcptr)(int,int)
, and the address of the add() function is then stored in funcptr. This indicates that the address of the add() function is in funcptr.
Now, we can use funcptr to invoke the add() function. The add() function is called by the expression funcptr(2,4)
, and the output is saved in the sum variable.
For that let us write a C++ program in order to print the name using a function pointer.
We use the static_ cast operator in order to output the contents of a void pointer. It changes the pointer's data type from its original void* type to that of the address it is storing:
#include <iostream>
using namespace std;
void printname(char *name)
{
std::cout << "Name is :" <<name<< std::endl;
}
int main()
{
char s[20]; //the array declaration
void (*ptr)(char*); // function pointer declaration
ptr=printname; // storing the address of printname in ptr.
std::cout << "Please enter the name of the person: " << std::endl;
cin>>s;
cout<<s;
ptr(s); // calling the printname() function
return 0;
}
Output:
Please enter the name of the person: Lali Name is Lali
The function printname(), which has the char pointer as a parameter, is defined in the program above. The function pointer is declared as void (*ptr)(char*). To assign the address of the printname() function to ptr, use the syntax ptr=printname. Now, we can use the ptr statement to invoke the printname() function (s).
As demonstrated here, we can pass the function pointers as arguments in our programs.
#include <iostream>
using namespace std;
void function1()
{
cout<<" The function1 is called";
}
void function2(void (*funcptr)())
{
funcptr();
}
int main()
{
function2(function1);
return 0;
}
Output:
The function1 is called
In the preceding program, we gave a function pointer to the function2() function as an argument. The main() method gives the func2() function the address of function1(). In this way, the function2() function indirectly calls the function1() function.
Function pointers were covered in this article. We've seen the value of function pointers, where they're utilized, and how they make our programs easier to design and maintain.