Link to home
Start Free TrialLog in
Avatar of thanesh
thanesh

asked on

What is a callback function and how it is used

Hi Experts,

I am trying to learn about callback function in C.  But, I couldn't find a simple example in the web.  Could you tell me what is it and how it is used.
Thanks,
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

A callback function is a function that is not called directly by you but it made buy another function. To acomplish that you pass the last one a pointer to the callback function. 'Callback' is a term used frequently in Windows, there are many examples in the internet, but if you want a pure C Language example, just have to search the "Pointers to functions" topic.
Typical example is the sort algorithm, where you can specify a sort method passing a pointer to function with the desired method.
Have a look to this tutorial page (better english than mine):
http://www.codetoad.com/c_pointers.asp
Also take a look to this interesting site, all about pointers to functions in C/C++
http://www.function-pointer.org/
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi thanesh,

As jaime already pointed out, these are functions you pass as an argument to another object which will in turn invoke that function for you. The concept itself is completely independent of the language.

A good example would be a pushbutton in a graphical user interface (GUI). You create the button on the screen and pass in a function to call whenever the button is pressed.
I am not too familiar with Windows, but in X/Motif this concept is used for all types of widgets. Take a look at the Pushbutton widget at http://w3.pppl.gov/misc/motif/MotifRefGuide/en_US/XmPushButtA.3X.html and you will see.


======
Werner
If you want a concrete Windows example, have a look to this code:
http://simplesamples.info/Windows/EnumWindows.php
It uses the WinAPI's EnumWindows() function, which calls-back a function called there EnumWindowsProc() and list information about every currently opened window.
http://simplesamples.info/Windows/EnumWindows.php
Avatar of thanesh
thanesh

ASKER

Thank you all of you for your input.  I still have a question.  What is then "registering a callback function" means?  

For example if I need to read some data from a socket and need to use callback how would it be useful.  What will be the benifit of using callback for this particular example?

Thank you,

Avatar of Kent Olsen

Some applications are very large and complex with a lot of callback functions.  Instead of passing all of the callback functions on the parameter list every time a function is called, registering the callback sets the correct callback function once and allows the other function calls to look pretty "normal".

Windows programming has specific APIs established to do this.  But you can perform your own "registering" of your callback function in your application a lot more simply.  In the example above, instead of passing 'my_callback' to the function, declare 'callback' to be a global variable, execute "callback = my_callback;", and simplify function header to call_n_times() to be call_n_times(int times).


Kent
thanks for the points...

just to clarify a "bug" in my code snippet above:

// Forgot to allow to pass in the callback argument for the function!
void call_n_times( int times, void(*callback)(void*), void *uarg ) {
    int i;
    for( i = 0; i != times; i++ ) {
        callback( uarg );
    }
}

// define the callback
void my_callback( void *arg ) {
    char *mystr = arg;
    printf( "%s\n", mystr );
}

int main(void) {
    char *arg = "hello world";
    // Forgot to pass the callback argument!
    call_n_times( 10, my_callback, arg );
}