Solved

What is a callback function and how it is used

Posted on 2004-09-29
8
1,622 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

706 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

20 Experts available now in Live!

Get 1:1 Help Now