What is the difference between Function Pointer vs Function Call?

16,651

Solution 1

Now the question is why should I choose function pointer?
What are the benifits will I get with function pointer?

You use function pointers when you need to implement a Asynchronous mechanism.
You need a function be called asynchronously when something happens.
How will you know which function to call?
The address of every function is Unique,So you need to use and store the function address.
Where do you store this function address?
A function pointer

Solution 2

There are many reasons to use a function pointer, in particular for doing things generically in C.

The main place you'll see them being used are as an argument to a function. For example with the function bsearch, it uses a comparison function, passed as a function pointer, to compare items and sort the data:

void *bsearch(const void *key, const void *base,
                     size_t nmemb, size_t size,
                     int (*compar)(const void *, const void *));

That allows bsearch to be generic and sort any type of data, since only that comparison function has to know the type of the data.


Another one has to do with avoiding multiple checks. ie.

void makePizza(void) { }

void bakeCake(void) { }

iWantPizza = 1;
...
if (iWantPizza)
   makePizza();
else
   bakeCake();
/* want to call the same function again... */
if (iWantPizza)
   makePizza();
else
   bakeCake();

..

/* alternative */
void (*makeFood)(void) = iWantPizza ? makePizza : bakeCake;

makeFood();
/* want to call the same function again... */
makeFood();

Solution 3

For the example that you showed, one more thing can be done.

Lets say there are bunch of function that needs to run for some device operation. In simple way, you can write all function calls in another master function and call that master function.

Another way to do it is, write all function names in a curly bracket and call each by using a function pointer and a loop. That looks smart. I'm not sure how that helps you in better way but I saw this in linux kernel code.


I agree to all the answers here. Apart from this I have some of my own judgements to use function pointer.

Lets take an example of some complex math calculation (like printing Fibonacci, integration, fourier Xform, etc...).

You have a function FX(which does that complicated math calculation or anything else) that you use many a times in your program. This function is used in many different jobs.

After using your program for a few months, you find out that, for some work, you can improve the function and for some, current one is best. What you will do? Write a new function, go and change the function name at all places.

Everytime you find something better, you are gonna do same.

Instead, use different function pointer for different work. At initial stage, all pointers can point to one function. When you discover a better function for some work, just divert the pointer and you are done.


Take another scenario. Here, you have a real big code like mobile phone OS. (not fully open but half compiled). You need to add bluetooth driver to it for a particular hardware.

Now, you can add or you can leave is the option available in OS.

You may need to turn on/off bluetooth from many places.

So what OS does is, it makes a function pointer that turn bluetooth ON and use it wherever it is needed. This code is already compiled so you cannot add your code in it. But what can be done is, you can write function and make that pointer point to your function.

This is what I have already seen under Android OS. (not exactly but nearer)

Solution 4

In my experience, function pointers are mainly used to pass a function as a parameter to another function.

Looking at your code, they could also be used like with arrays, so you can just loop through the entire array (which could consist of hundreds of function pointers) and it will just execute them all.

Solution 5

Function Pointers are pointers(like variable pointers) which point to the address of a function. They actually calling the underlying function when you dereference them like a function call does.

The main advantage of a function pointer is that you can pass it to another function as a parameter for example ...

Share:
16,651
Rasmi Ranjan Nayak
Author by

Rasmi Ranjan Nayak

Always ready for programming. Favorite Programming Languages C, C++, Python, Java, Android

Updated on June 16, 2022

Comments

  • Rasmi Ranjan Nayak
    Rasmi Ranjan Nayak almost 2 years

    Hello Friends,
    How can I use an array of function pointers?
    If we will see the above link, it tells us how function pointer works.
    Now the question is why should I choose function pointer?
    Why can't I use function call directly?
    What are the benifits will I get with function pointer?
    e.g

    enum{a, b, c};
    void A();
    void B();
    void C();
    void (*fun[3])() = {A, B, C};
    
    Now when I need to call a function I am doing like,
    fun[a](); // This calls function A
    // And so on...
    

    Same can be done in function calls also

    like when I need to call function A, B or C. directly I can right like

    A();
    B();
    or 
    C();
    

    Then why function pointer?

  • johnny
    johnny almost 10 years
    I really like the example with the complex math calculation.