Function Pointers

A pointer to a function contains the address of the function in memory.

We know that an array name is really the address in memory of the first element of the array.Similarly, a function name is really the starting address in memory of the code that performs the function’s task.Pointers to functions can be passed to functions, returned from functions,stored in arrays and assigned to other function pointers.

Examples for Function Pointers in C++ are the following:

Multipurpose Bubble Sort Using Function Pointers

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<iomanip>
using std::setw;
void bubble(int [],const int,bool(*)(int,int));
void swap(int*const,int *const);
bool ascending( int, int);
bool descending( int, int);
int main()
{
const int arraySize=10;
int order;
int counter;
int a[arraySize]={2,6,4,8,10,12,89,68,45,37};
cout<<"Enter 1 to sort in ascending order,\n"
    <<"Enter 2 to sort in descending order,\n";
cin>>order;
cout<<"\nData items in original order\n";
for(counter=0;counter<arraySize;counter++)
cout<<setw(4)<<a[counter];
if(order==1)
{
bubble(a,arraySize,ascending);
cout<<"\nData items in ascending order\n";
}
else
{
bubble(a,arraySize,descending);
cout<<"\nData items in descending order\n";
}
for(counter=0;counter<arraySize;counter++)
cout<<setw(4)<<a[counter];
cout<<endl;
return 0;
}
void bubble(int work[],const int size,bool(*compare)( int, int))
{
for(int pass=1;pass<size;pass++)
for(int count=0;count<size-1;count++)
if((*compare)(work[count],work[count+1]))
swap(&work[count],&work[count+1]);
}
void swap(int *const element1Ptr,int *const element2Ptr)
{
int hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
bool ascending(int a, int b)
{
return b<a;
}
bool descending(int a, int b)
{
return b>a;
}

OUTPUT:

Enter 1 to sort in ascending order,
Enter 2 to sort in descending order, 
2 Data items in original order                                                             2   6   4   8  10  12  89  68  45  37                                                
Data items in descending order                                                           89  68  45  37  12  10   8   6   4   2  

Arrays of Pointers to Functions

One use of function pointers is in the menu-driven systems. The program prompts a user to select an option from the menu. Each option is serviced by a different function. Pointers to each function are stored in an array of pointers to functions. In this case, all the functions to which the array points must have the same return type and same parameter types. The user’s choice is used as a subscript into the array of function pointers, and the pointer in the array is used to call the function.

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
void function1(int);
void function2(int);
void function3(int);
int main()
{
void(*f[3])(int)={function1,function2,function3};
int choice;
cout<<"Enter a number between 0 and 2,3 to end:";
cin>>choice;
while(choice>=0&&choice<3)
{
(*f[choice])(choice);
cout<<"Enter a number between 0 and 2,3 to end:";
cin>>choice;
}
cout<<"Program execution completed"<<endl;
return 0;
}
void function1(int a)
{
cout<<"You entered"<<a
    <<"so functional was called\n\n";
}
void function2(int b)
{
cout<<"You entered"<<b
    <<"so function2 was called\n\n";
}
void function3(int c)
{
cout<<"You entered"<<c
    <<"so function3 was called\n\n";
}

OUTPUT:

Enter a number between 0 and 2,3 to end:1
You entered1so function2 was called 

Translate »