Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Function Pointer in C++

Last Updated on April 19, 2023 by Prepbytes

Function pointers in C++ are one of the unique features of the language. The function pointer in C++ points to a function, and it can be used to invoke the function at runtime. C++ Function pointers can be very useful when it comes to writing complex programs or designing dynamic libraries. Let us study the C++ Function Pointers in detail.

What is Function Pointer in C++?

A function pointer in C++ is a variable that stores the memory address of a function. In C++, a function is treated like any other variable, and it can be passed as an argument to another function or returned as a value from a function. When a function is passed as an argument to another function, it is referred to as a C++ function pointer.

How to Declare a Function Pointer in C++?

To declare a function pointer in C++, we use the following syntax:

return_type (*pointer_name)(argument_type1, argument_type2, ...);

Example of How to Declare Function Pointer in C++
If we want to declare a C++ function pointer that points to a function that takes two integer arguments and returns an integer, we would use the following syntax:

int (*func_ptr)(int, int);

Here, func_ptr is a pointer to a function that takes two integer arguments and returns an integer. We can use this pointer to call the function at runtime.

How to Assign a C++ Function Pointer to a Function?

Once we have declared a function pointer, we can assign it to a function using the following syntax.

pointer_name = function_name;

Example of Assigning C++ Function Pointer to a Function:
If we have a function called addNumbers that takes two integer arguments and returns their sum, we can assign its address to a function pointer using the following line of code.

int (*addNumbers_ptr)(int, int) = &addNumbers;

Here, we are assigning the address of the addNumbers function to the addNumbers_ptr pointer. Now, we can use this pointer to call the addNumbers to function at runtime.

How to Call a Function Using a Function Pointer in C++?

To call a function using a function pointer in C++, we use the following syntax:

(*pointer_name)(argument1, argument2, ...);

Example:
If we want to call the addNumbers function using the addNumbers_ptr pointer, we will use the following line.

int result = (*addNumbers_ptr)(2, 3);

Here, we are calling the addNumbers function using the addNumbers_ptr pointer and passing two integer arguments. The result variable will store the sum of these two integers.

Examples of Function Pointer in C++

Now let us understand the Function Pointer in C++ with the help of the following examples.

Example 1 of C++ Function Pointer
Using a function pointer in C++ to pass a function as an argument to another function.

Code:

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

// Function that accepts a function pointer as an argument
void operate(int (*operation)(int, int), int x, int y) {
    int result = (*operation)(x, y);
    cout << "Result: " << result << endl;
}

int main() {
    // Declare a function pointer 
    int (*add_ptr)(int, int) = &add;
    
    operate(add_ptr, 2, 3);

    return 0;
}

Output:

Result: 5

Explanation:
In this example, we have defined a function called add that takes two integer arguments and returns their sum. We have also defined a function called operate that accepts a function pointer as an argument and calls it with two integer arguments. In the main function, we have declared a function pointer in C++ called add_ptr that points to the add function. We then call the operate function and pass the add_ptr function pointer as an argument, along with two integer arguments. The operate function calls the function pointed to by the function pointer with the two integer arguments and prints the result to the console. I.e., “Result: 5”.

Example 2 of C++ Function Pointer
Using a function pointer to return a function from another function

Code:

#include <iostream>

using namespace std;

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }

// Function that returns a function pointer
int (*get_operation(char op))(int, int) {
    switch (op) {
        case '+':
            return &add;
        case '-':
            return &subtract;
        case '*':
            return &multiply;
        case '/':
            return &divide;
        default:
            return NULL;
    }
}



int main() {
    int (*operation)(int, int) = get_operation('+');
    int result = (*operation)(2, 7);
    cout << "Result: " << result << endl;

    return 0;
}

Output:

Result: 9

Explanation:
In this example,

  • We have defined a function called “get_operation” that takes a character as an argument and returns a function pointer to the corresponding arithmetic operation function (i.e., add, subtract, multiply, or divide).
  • We have also defined four functions to perform basic arithmetic operations.
  • In the main function, we declare a function pointer called operation and call the get_operation function to assign it to the add function.
  • We then call the function pointed to by the function pointer with two integer arguments and print the result to the console.
  • The get_operation function uses a switch statement to determine which arithmetic operation function to return based on the input character, and returns NULL if an invalid character is passed as an argument.

Finally, we get output as “Result: 9” on the screen as we passed the 2 and 7 as parameters.

Conclusion
In conclusion, The Function Pointer in C++ allows us to pass functions as arguments to other functions or return functions as values from functions. Function pointers can be used to design dynamic libraries or write complex programs that require runtime function invocation. In this article, we learned about the C++ Function Pointers, how to declare them, assign them to functions, and call them at runtime.

Frequently Asked Questions(FAQs)

Here are some Frequently Asked Questions related to “Function Pointer in C++”.

Ques 1. What is the purpose of using function pointer in C++?
Ans. Function pointer in C++ is useful in many ways, such as calling a function dynamically at runtime or passing a function as an argument to another function.

Ques 2. Can you pass a function as an argument to another function in C++?
Ans. Yes, you can pass a function as an argument to another function using C++ function pointers.

Ques 3. How do you call a function using a function pointer in C++?
Ans. You can call a function using a function pointer by dereferencing the pointer and invoking it with appropriate arguments.

Ques 4. What is a callback function?
Ans. A callback function is a function that is passed as an argument to another function and is called by that function.

Ques 5. How do you return a function pointer in C++?
Ans. You can return a function pointer from a function using the same syntax as a normal pointer.

Leave a Reply

Your email address will not be published. Required fields are marked *