• C

CallBack functions

what is advantage of using call back fuction instead of normal functions.
when we can use normal functions why we have to use call back functions
Who is Participating?
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>>  if callback function is a normal function why we have to go for it
You don't have to do anything.

Ok, look at it this way... a door can be open or it can be closed but in either situation it is still a door. Same with a callback function. It can be called directly or indirectly (via a pointer as a callback) but it is still just a function. The term "callback" refer to how you call the function it does not imply a special type of function.

>> Do you mean defination of  call back function changes for different scenarios.
As a question there is no real sensible answer to this because it implies that if a function is being used as a callback it must have some special properties. This isn't the case. It's just a function -- called via a pointer.

>> please explain with a code snippet  
Sure, see below.

#include <stdio.h>

typedef void (*func_ptr)();

void foo()
   puts("hello, world");

void bar(func_ptr callback)
   /* performing a function callback */

int main()
   /* calling foo direct */

   /* calling foo via a pointer */
   func_ptr fp = foo;

   /* passing the functon pointer to bar() so it can be used as a callback */


   return 0;

Open in new window

evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
There is no specific advantage per se.

A callback function is just a function that is called indirectly via a pointer. The difference is how and when it is called. With a callback the function is passed by pointer into some other thread of code. That other code can then call the "callback" function via this pointer. This allows you to decide what the function is that gets called.

Think of it as a phone number that you give to someone. Depending on the number, when they call it they will get a different person. With the function pointer, then they call it they will get a different function depending on which function's address you passed in.

A very good tutorial on function pointers: http://www.newty.de/fpt/index.html
Prime usecase for callback functions is asynchronous communication. Where as "normal" functions are used in synch communication (single thread).
E.g. you write a small library that hides all the complexity of C-socket APIs and provides a more user friendly set of functions. Idea being that you call one function from this library and provide all connection params (like socket num, IP,...) and a callback function pointer. Then that function will take care of setting up the connection & monitors incoming data by setting up a thread, when the data comes it'll call back your function via pointer.
Given what evilrix described, now this library can be used by anyone.

In OO design, callback functions are used to break circular dependencies.
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

The other point, implicit in the phone analogy, is that not only do you get to choose *how* your code will respond to the call back (by selecting which function to pass for the call back) but it also allows the "other" code (usually the operating system, or some package that was bought) to determine *when* to call your function.

This can be used for interim progress reports on the task it is working on. Or, in the operating system case, it is often explicitly used to manage a timer. Whenever a time interval expires, it can call back to your code to notify you of that.
Well, there's no one way I can think to describe Callback functions:

They are a way of implementing something today that will work with something that won't exist until the future.  

They are a different way of implementing reusable code.  

At a concept level, they are an efficient way of implementing run-time dynamic linking without the overhead that normally comes with run-time linking.

A perfect example would be the generic sorting function QuickSort().  When it was written, the programmer knew what sorting algorithm he wanted to implement.  But he didn't know what would be sorted.  So there was no way that logic could be written into QuickSort to compare items to determine which comes first in the sort.  So a Callback function was designed into QuickSort so the user could supply the missing logic that compares the items to being sorted.

So utilizing a Callback function, a programmer was able to write a sorting algorithm without knowing what was getting sorted, and another programmer could sort his data without knowing HOW to implement sort logic (by supplying the compare logic via a Callback).  Effectively two pieces of logic got "linked' together at run-time by simply supplying a pointer to a Callback function.
nagaharikolaAuthor Commented:
I am not clear on this point yet.
how it is different from normal function.
evilrixSenior Software Engineer (Avast)Commented:
>> I am not clear on this point yet.
>> how it is different from normal function.

As I said in my [the] very first comment... it's not different.


"A callback function is just a function that is called indirectly via a pointer. "

Do you understand what a pointer is, what a pointer to a function is and what it means to call a function via a pointer?
nagaharikolaAuthor Commented:
yes i understand that.
the link given is not clear for me.
evilrixSenior Software Engineer (Avast)Commented:
>> the link given is not clear for me.
Specifically, what is not clear? What don't you understand?
>> I am not clear on this point yet.
>> how it is different from normal function.
Think of it more like a role that a function can play.

E.g. take a number 12. It's a number, but it could be my birth date or it could be distance in miles from your home to your office or it could be number of volleyball players allowed in a team.
So its the same, but it can play many roles.

Another example: A cop can be:
- a father
- a friend
- a traffic cop
- a federal cop
- ...

A function is just a function, when you use it in some special cases (e.g. the passing it as a param to a sort algorithm) it acts as a callback function. So a callback function is also a "normal function".
nagaharikolaAuthor Commented:
>>callback function is also a "normal function".
   if callback function is a normal function why we have to go for it.

Do you mean defination of  call back function changes for different scenarios.
please explain with a code snippet  

nagaharikolaAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.