Function Pointer In C++

In C++, a function pointer is a type of pointer thacpp tutorials to a function instead of a data value. It enables functions to be passed as arguments, stored in arrays, or returned from other functions, which enables dynamic function calls and flexible programming structures, such as callbacks and function tables.

In C++ , pointers are used to point to some variables; similarly, the function pointer is a pointer used to point to functions. It is basically used to store the address of a function. We can call the function by using the function pointer, or we can also pass the pointer to another function as a parameter. They are mainly useful for event-driven applications, callbacks, and even for storing the functions in arrays.

What is the Address of a Function?

As we know, the computer only understands the low-level language, i.e., binary form. In C++, every function in a program is mainly stored in the main memory (RAM) after compilation. We can use the compiler to convert the high-level source code into the machine code that is stored in an executable file. This executable file gets stored in RAM. The CPU starts the execution from the main method, and it reads the copy in RAM but not the original file.

All the functions and machine code instructions are data. This data is a bunch of bytes, and all these bytes have some address in RAM. The function pointer contains the RAM address of the first instruction of a function.

Declaration of Function Pointer

The following syntax shows the declaration of a function pointer:

Example

return_type (*Pointer_name) (parameter_list);

In this syntax,

  • *Pointer_name: It represents the name of the pointer.
  • Parameter_list: It represents the list and name of the arguments.
  • Getting the Address of a Function

In C++, we can get the address of a function very easily. If we want to get the address of a function, we just need to mention the name of the function, and we don't need to call the function.

C++ Example of Getting the Address of a Function

Let us take an example to illustrate how to get the address of a function in C++.

Example

Example

#include <iostream>  

using namespace std;  //using standard namespace

int main()  //main function

{  

  cout << "Address of a main() function is : " <<&main<<endl;  

  return 0;  

}

Output:

Output

Address of a main() function is : 1

Explanation:

In the above program, we are displaying the address of a main function. In order to print the address of a main function, we have just mentioned the name of the function; there is no bracket and no parameters. Therefore, the name of the function by itself without any brackets or parameters means the address of a function.

Calling the Function Indirectly using the Function Pointer

In C++, we can call the function with the help of a function pointer by simply using the name of the function pointer. The syntax of calling a function through a function pointer is similar to the way we normally call a function.

C++ Example to Call the Function Indirectly using the Function Pointer

Let us take an example to illustrate how to call the function indirectly using the function pointer.

Example

Example

#include <iostream>  

using namespace std;  //using standard namespace

int add(int a , int b)  

{  

    return a+b;  

}  

int main()   //main function

{  

 int (*funcptr)(int,int);  // function pointer declaration  

 funcptr=add; // funcptr is pointing to the add function  

 int sum=funcptr(5,5);  

 cout << "value of sum is : " <<sum<<endl;  

  return 0;  

}

Output:

Output

value of sum is : 10

Explanation:

In this example, we declare the function pointer, i.e., int (*funcptr)(int,int), and then we store the address of add function in funcptr. It implies that funcptr contains the address of the add function. Now, we can call the add function by using funcptr. Finally, the statement funcptr(5, 5) calls the add function, and the result of the add function gets stored in the sum variable.

Using Function Pointer with a String

In C++, function pointers are not restricted to numeric operations, but they can also be utilized with any function signature, including those that take char* or std::string types. It is very useful to handle operations on strings, including custom printing, parsing, formatting, etc.

C++ Function Pointer with a String Example

Let us take an example to illustrate the function pointer with a string in C++.

Example

Example

#include <iostream>  

using namespace std;  //using standard namespace

void printname(char *name)  

{  

    cout << "Name of the person is : " <<name<<endl;  

}  

  

int main()  //main function

{  

    char s[20];  // array declaration  

    void (*ptr)(char*);  // function pointer declaration  

    ptr=printname;

    cout << "Enter the name of the person: ";  

    cin>>s;  

    ptr(s);  // calling printname() function  

   return 0;  

}

Output:

Output

Enter the name of the person: John

Name of the person is : John

Explanation:

In this example, we define the function printname that contains the char pointer as a parameter. After that, we declare the function pointer, i.e., void (ptr)(char). The statement ptr=printname means that we are assigning the address of printname function to ptr. Finally, we can call the printname function by using the statement ptr(s).

Array of Function Pointers

In C++, an array of function pointers is a collection where every element is a pointer to a function. It enables the dynamic function selection and helps to call functions based on index or condition.

Syntax

It has the following syntax:

Example

return_type (*array_name[])(parameter_list);

C++ Array of Function Pointers Example

Let us take an example to illustrate array of function pointers in C++.

Example

Example

#include <iostream>

using namespace std;  //using standard namespace

// Define some functions

int addition(int x, int y) { return x + y; }

int subtract(int x, int y) { return x - y; }

int multiply(int x, int y) { return x * y; }

int main() {  //main function

    // Declare an array of function pointers

    int (*operations[3])(int, int) = {addition, subtract, multiply};

    int a = 20, b = 18;

    cout << "Addition: " << operations[0](a, b) << endl;

    cout << "Subtract: " << operations[1](a, b) << endl;

    cout << "Multiply: " << operations[2](a, b) << endl;

    return 0;

}

Output:

Output

Addition: 38

Subtract: 2

Multiply: 360

Explanation:

In this example, an array of function pointers operations is used to store three arithmetic functions: addition, subtract, and multiply. In the array, every function pointer is called with two integer arguments (a and b), which allows dynamic invocation of the respective operations.

Pass a Function Pointer as a Parameter

In C++, the function pointer can be passed as a parameter to another function to allow dynamic behavior. If we want to perform this operation, we have two main methods. First, either pass the value that we get, and second, pass the function pointer that already exists in the program.

C++ Pass a Function Pointer as a Parameter Example

Let us take an example to illustrate how to pass a function pointer as a parameter in C++.

Example

Example

#include <iostream>  

using namespace std;   //using standard namespace

void func1()  

{  

    cout<<"func1 is called";  

}  

void func2(void (*funcptr)())  

{  

    funcptr();  

}  

int main()   //main function

{  

  func2(func1);  

  return 0;  

}

Output:

Output

func1 is called

Explanation:

In this example, the func2 function takes the function pointer as a parameter. After that, the main method calls the func2 function in which the address of func1 is passed. In this way, the func2 function is calling the func1 indirectly.

C++ Function Pointer MCQs

1) What is the Function Pointer in C++?

  • It is a pointer that stores the address of a class object
  • It is a pointer that stores the address of a function
  • It is a pointer that stores the address of a variable
  • It is a pointer that stores the return value of a function

2) Which of the following options shows the correct syntax of function pointer in C++?

  • int *func(int, int);
  • int (*funcptr)(int, int);
  • int funcptr(int, int);
  • int &funcptr(int, int);

3) What will be the output of the following code?

Example

#include <iostream>

using namespace std;

void CppTutorial() { cout << "CppTutorial"; }

void Tech() { cout << "Tech"; }

int main() {

    void (*fptr[2])() = {CppTutorial, Tech};

    fptr[1]();

    return 0;

}
  • Tech
  • Cpp Tutorial
  • CppTutorial
  • CppTutorial, Tech

4) Which one of the following option is not a correct use of function pointers in C++?

  • Storing in an array
  • Passing as an argument to another function
  • Calling a constructor
  • Returning a function from a function

5) What will be the output of the following code?

Example

#include <iostream>

using namespace std;

void func(int a)

{

    cout << a;

}

int main()

{

    void (*n)(int);

    n = &func;

    (*n)( 5 );

    n( 5 );

    return 0;

}

Input Required

This code uses input(). Please provide values below: