简体   繁体   中英

What is the difference between Function Pointer vs Function Call?

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?
eg

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 ?

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();

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)

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

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.

What is the difference between Function Pointer vs Function Call?

It's like the difference between asking the compiler to "tell me the address of the National Gallery (I might want to go there later and I want to be ready to do it)", rather than "take me to the National Gallery right now (but I won't be paying attention to how you get me there so don't expect me to know later on)". Crucially, if you ask for the address/pointer you can write it down in some place like "next Sunday afternoon's big trip"... you don't even have to remember that it is the National Gallery you'll be going to - it can be a pleasant surprise when you get there - but you immediately know your Sunday's entertainment's all sorted.

What benefits will I get with function pointer?

Well, as above, at the time you set the function pointer you need to make a decision about where you'll call later, but then you can forget about all the reasons for making that decision and just know that later destination's all ready for use. At the time when you're actually doing stuff... "my Sunday routine: sleep in to 10, eat a big breakfast, go back to bed, have a shower, if I've got plenty of money then go on my Sunday afternoon big trip, meet friends for dinner"... the earlier decision just kicks in to get you to the gallery. Crucially, you can keep using your familiar Sunday schedule and start "pointing" the "next Sunday afternoon's big trip" address/pointer at new places as they catch your eye, even if they didn't exist when your general schedule was formed.

You see this post-facto flexibility to change the destination dispatched to at one step in an old routine illustrated well by AusCBloke's mention of bsearch . qsort is another classic example - it knows the big picture logic of efficiently sorting arrays of arbitrary things, but has to be told where to go to compare two of the things you're actually using it for.

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 ...

These are good examples, particularly the first in Urvish's above. I have wondered the same thing, and I think the answer is purely design. In my mind, they are the same result, as in you can point to a function and get a+b or you can just call a function regularly and get a+b, and with the examples on SO, they are usually small and trivial for illustration. But , if one had a 10k line C program, and you had to change something fifty times because you made a change, you'd probably pick up pretty quickly why you'd want to use function pointers.

It also makes you appreciate the design of OOP languages and the philosophy behind OOD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM