Solved

What is a callback function and how it is used

Posted on 2004-09-29
8
1,624 Views
Last Modified: 2010-04-15
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,
0
Comment
Question by:thanesh
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12182058
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12182068
Also take a look to this interesting site, all about pointers to functions in C/C++
http://www.function-pointer.org/
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 125 total points
ID: 12182092
A callback function is an argument to a function that allows a application to specify a function that is required to be called. This argument is what is called a "function pointer", in that it is a pointer to a function. unlike other pointers in C, you don't have to specify "&" to get the function pointer, just the function name.
The complex part comes when you start defining the function pointer parameter type...

a simple example is as follows:

// simply call the callback N times...
void call_n_times( int times, void(*callback)(void) ) {
    int i;
    for( i = 0; i != times; i++ ) {
        callback();
    }
}
   
extending the example with a "void *" user argument allows the callback function to be much more powerful, as it can get specific data related to itself passed back with the call...

void call_n_times( int times, void(*callback)(void*) ) {
    int i;
    for( i = 0; i != times; i++ ) {
        callback();
    }
}

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

int main(void) {
    call_n_times( 10, my_callback );
}
0
 
LVL 11

Expert Comment

by:griessh
ID: 12182183
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
0
Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12182251
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
0
 
LVL 1

Author Comment

by:thanesh
ID: 12182538
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,

0
 
LVL 45

Expert Comment

by:Kdo
ID: 12182996

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
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12197914
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 );
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now