Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What is a callback function and how it is used

Posted on 2004-09-29
8
Medium Priority
?
1,631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 46

Expert Comment

by:Kent Olsen
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

609 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