C++ Laguage



Admission Enquiry Form

  

Pointer to Function in C++


Pointer to function in C++

//pointer to function
#include<iostream>
using namespace std;
void sum()
{
int num1,num2,res;
num1=10;
num2=20;
res=num1+num2;
cout<<"\n The sum is "<<res;

}//end of sum
void subtract()
{
int num1,num2,res;
num1=100;
num2=20;
res=num1-num2;
cout<<"\n The subtraction is "<<res;
}
int main()
{
void (*fun_ptr)();
fun_ptr=&sum;
cout<<"\n now calling sum function through pointer";
fun_ptr();
fun_ptr=&subtract;
cout<<"\n now calling subtraction function through pointer";
fun_ptr();
}//end of main function