Tutorial Study Image

Function Pointers in C++


October 8, 2022, Learn eTutorial
1461

What is a function pointer?

  • A function pointer is a  kind of variable that mainly holds the address of a function that can be invoked later using that function pointer.
  • We already learned about passing the arguments to a function in the C++ Functions tutorial. Due to the fact that the actual value is passed, this method is referred to as passing by value.
  • Pointers, as we all know, they are used to point to the variables. In the same way, a  pointer that is used to point to functions seems to be the function pointer.
  • The actual values of the arguments are not at all passed in this method, however, there is a different way to pass arguments to a function. As an alternative, a reference to the values is passed.

Syntax

The syntax for declaring a function pointer


int *funcPtr(int, int)
 
  • The function declaration is made using the syntax above. Although functions are more complex than variables, function pointers in C++ are type safe hence the function pointers have a return type as well as a parameter list.
  • In the above-mentioned syntax, the return type is supplied very first, followed by the name of the pointer which means  FuncPtr, which is enclosed in brackets and is preceded by the (*) pointer symbol.
  • We have then provided the parameter list (int, int).   The function pointer in the above syntax can point to any function that takes two integer parameters as well as it returns an integer type value.

What do you mean by the address of a function?

What do you mean by the address of a function

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.

Address of a Function in C++

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

  • The address of a main() function is shown in the program above.
  • We have only mentioned the function's name and its address in order to print the address of the main() function; there are no brackets or parameters.
  • As a result, the address of a function is indicated by the function name alone, without any brackets or parameters.
  • We can use the alternative method, &main, in order to print the address of the function.

How can we call a function?

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.

How we will print the name using the function pointer?

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).

How can you pass the function pointers as arguments in C++?

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.

You should be aware of the following facts about function pointers:

  • A function pointer doesn't point to data like other pointers; instead, it points to program code. A function pointer is typically used to store the start of executable code.
  • Unlike normal pointers, we don't utilize function pointers to allocate or free up memory.
  • it is also possible to utilize a function's name to determine the address of that function.
  • The same rules that apply to normal pointers also apply to an array of function pointers.
  • Use of function pointers can be used in place of switch cases.
  • Similar to how a data pointer can be passed as an argument and returned from a function, a function pointer can do the same.

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.