Raulp
Raulp

Reputation: 8136

In C Why function pointers are better suited in function call backs?

I know function call back is embedding another function pointer in the call of another function like this:

Function Declaration:

function_call_back(int x, int y, void (*functonPtr)(int x, char y , struct newStruct* sptr))


void func(int x , char y , struct newStruct* sptr1)
{
    //perform Algorithm 1 on x,y ,sptr1
}

void func2(int x , char y , struct newStruct* sptr2)
{
    //perform Algorithm 1 on x,y ,sptr2
}


void func3(int x , char y , struct newStruct* sptr3)
{
    //perform Algorithm 1 on x,y ,sptr3
}

void func4(int x , char y , struct newStruct* sptr4)
{
    //perform Algorithm 1 on x,y ,sptr4
}

main()
{
    // function calling 
    function_call_back(23, 2256, func1);
}

Here the third argument is func, as function name is equivalent to function pointer

I agree that the func here can be altered with different variation of similar function signature by adding this line in main above before call to function_call_back:

 typedef void (*fptr)(int int x , char y , struct newStruct* ptr);

  fptr f1 = func2; // or func3, or func4

  function_call_back(23, 2256, f1);

The third argument is func, as function name is but I was wondering this can be achieved in the below way also, by simply adding the function-calling code in the call to function_call_back:

function_call_back(23, 2256, functionCallingcode); //third argument is func 

The new declaration of function_call_back is:

function_call_back(int x, int y, int functionCallingCode)

And its new definition is:

void function_call_back(int x, int y, int functionCallingCode)
{
switch(functionCallingCode)
case 1:
func1(1,"g",sptr1);
break;

case 2:
func2(1,"c",sptr2);

break

case 3:
func3(1,"d",sptr3);
break;

case 4 : 
func4(1,"s",sptr4);
break ;

default : 
 printf("Wrong Function calling code");

}
}

Then why to use function pointer?

Upvotes: 0

Views: 417

Answers (3)

Kerrek SB
Kerrek SB

Reputation: 476950

With the switch statement, you can call every function that you choose to code into your callback function. You are guaranteed that only one of your known collection of functions can be called.

With the function pointer, you can call any function that you may define at a later and completely unrelated point in your development. You're free to define and use any callback function with a matching signature.

In other words, the switch allows you to branch among a bounded set of choices, while the function pointer gives you unbounded flexibility. Usually there's little reason to prefer anything else over the function pointer.

Upvotes: 3

Nigel Harper
Nigel Harper

Reputation: 1250

In the case you describe you clearly can get the functionality you want without using a function pointer.

However consider what happens if you want to add the option of func5 in a few weeks time. And then as time goes by you want to add func6, func7, ..., func10, func20. function_call_back() gets bigger and messier whereas with the function pointer version you can add as many different funcs as you want without having to edit function_call_back() at all.

The function pointer implementation also gives you the flexibility to parcel function_call_back() up as part of a library that you or anyone else can use in another program without having to mess with, or even have, the source code. See the standard library qsort function for an example of why this is really handy.

Upvotes: 2

MByD
MByD

Reputation: 137282

They are not better, they are practically the only option you have.

You can do this switch, but in the same approach, you can just write the whole program in one functions. Functions are there to separate code units, and this "switch-function" just units too many things.

Upvotes: 3

Related Questions