Saturday, August 6, 2016

Function Pointers

Pre-requisites for going ahead with this tutorial :

  • Pointers Basics (Pointer to Int,Char)
  • Pointers and Arrays
     

Since we have already dealt with pointers well enough and seen their usage with primitive data types such as int,char and also with arrays, In this section , we will move one more step further and see the usage of pointers with functions.

Let us revisit it very briefly.

int value;       // value is a variable of an integer type

int *value;     // * value is a variable of an integer type or value is a pointer to an integer

This is how we read a pointer variable. The same holds good with a function pointer too.

We all know the basic prototype of a function declaration.

                      int operation(int , int);

The statement above is just a normal prototype of a function which accepts 2 integer values as their arguments and returns an integer.

                     int (*operation)(int,int);

The above statement can be read as- 


  • operation is a pointer to a function taking 2 integer arguments and returning an integer, or in simple words- operation is a function pointer with 2 integer arguments.

As a pointer to an integer stores the address of some other variable, similarly pointer to a function stores the address of some other function, or more precisely , the base address of some other function.


So, one question that may arise is that why do we need a function pointer? What is the use of it?

By now , you must have got the understanding that knowing the address of a variable does a lot of help in C programming and we can't just by-pass or ignore them. So is the function pointer. Knowing the address of a function helps a lot in C programming and saves a lot of efforts.

How?


It helps us in getting the flexibility while using functions. It makes our program look more short and precise and eliminates the overhead of calling the multiple functions multiple number of times with the same set of parameters.

Let us first see a basic example of the usage of function pointer.

Here ,operation is a pointer to a function which takes 2 integer arguments.

 
The function which it points to is bigger(int,int). As in case of a normal variable pointer where we can access the contents of a variable by dereferencing it, the same applies here, and the statement above the printf() statement shows how to dereference a function pointer. However, dereferencing can also be done by simply calling the function pointer without any *(asterisk) operator as below.
                                  int large= operation(2,3)

Output :


 
The above example is an extremely simple one which explains the basic usage of a function pointer. However, the same function pointer can be used to point to various other functions as well. 

Let's see one more example :

 
The function pointer stores the address of different functions depending upon the input value provided by the user. After the choice is made as to which function the function pointer is going to point to, we can dereference the function pointer with the same set of arguments.

Output :

 
Function Pointer as an argument to another function- We could have even passed the function pointer as one of the parameters into another function. Let's take our first example and modify it by passing the function pointer as an argument into a third function.

Instead of directly dereferencing the function pointer and storing the output in a variable, we are passing the function pointer into a third function and then storing the return value of this function in a variable.

 

Here , the function perform() accepts a function pointer as an argument and returns the value obtained as a result of the functionality of the (*operation)(int,int) function pointer which points to the address of the function bigger, and in turn returns the larger of the two passed values.

Output :

 
We can make use of typedef to make the above stuff look a little simpler. At the same time, usage of typedef may also seem to be confusing at the first sight. So, you have to get used to it.

Remember that by using typedef, you are eliminating the need to put in the data type and return type while passing the function pointer as an argument

Let's modify the above example :


Keep in mind the following thing to not get confused with typedef statement in the above scenario-

int (*operation)(int,int); // This is how you declare function pointer.

To make use of typedef , you don't need to get too much of trouble . You can just do the following:-

typedef int (*oper)(int,int); //The words in bold are all that you need to take care of.
 


After this , all you have to do is the following to declare a function pointer:-

oper operation;   //operation is analogous to a variable and oper is analogous to a data type for that variable


It's as simple as declaring a variable. Now , to point to any function, you just need to do that in the way you assign an address to a pointer variable.

operation=bigger; // It looks as if you are assigning the value of bigger to operation
operation=&bigger; // This is also the same, and it looks as if you are assigning the base address of bigger to the pointer variable operation

<<<Function pointer gives you so much of flexibility that at places, you can just include & or eliminate it, or use * or eliminate it.>>>

Output :

Such an application of a function pointer , of passing it as an argument, can be found in the standard library function qsort(3), which is used to sort an array.

Returning a function pointer from another function - Function pointers can also be returned from another function just like a normal variable is returned.
This looks even more complicated,but can again be simplified by making use of typedef.
We will take a look at how function pointers can be returned from another function :
The function pointer operation() points to the function bigger() or sum() depending on the parameter num in the perform() function. After pointing to one of the functions, this function pointer is returned from function perform().

Output :


So, This is all I have to offer from the section on Function Pointers. You are encouraged to explore more on this topic by looking at various examples which really help in code optimization and increase efficiency .

In case of any doubt or if you need any clarification on any of the topics, get in touch by posting a comment.