Function Pointer in C plus plus programming language

Function Pointer in C plus plus programming language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

Function Pointer in C plus plus programming language. The following code shows the use of function pointer in C plus plus Programming Language.

 

Code:

#include<iostream>
using namespace std;

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

int main()
{
    int total;
    // Function Pointer
    int (*p)(int, int);
    p = &add;
    total = (*p)(4, 5);
    cout << "Total is : " << total << endl;

    // Another way
    p = add;
    total = p(7, 9);
    cout << "Total is : " << total;

    return 0;
}

 

Output:

Total is : 9
Total is : 16

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram