Basic C question. What is a callback?

This is a discussion on Basic C question. What is a callback? within the C Programming forums, part of the General Programming Boards category; So I have started to look into some C API documentation and see this quite frequently, what is a callback ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Basic C question. What is a callback?

    So I have started to look into some C API documentation and see this quite frequently, what is a callback exactly.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I think it's when you calls someone up and they say they will call you back

    Actually I remember now, when you call someone and the line is engaged you can dial a callback
    code and then when they put the phone down both yours and their phone will ring.

    Anyway back to the question what API documentation is this?
    I find the term is often ill defined/ill used.
    Can you post the document?

  3. #3
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by esbo View Post
    I think it's when you calls someone up and they say they will call you back
    But they don't!
    OS: Linux Mint 13(Maya) LTS 64 bit.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    esbo, I have seen it in alot of different documentation. But the one I´m looking into at the moment is Core Audio which is part of mac os. The document is a couple of hundred pages but just as a taste of what I talking about here is a snippet.

    The following is a callback that an AudioUnit can make from its RenderSlice
    function. It can be used as a RenderNotify callback for the owner (usually the
    AUGraph) to get called back before and after rendering.
    It can also be used as an InputCallback to provide a buffer of audio inputto one
    of the AudioUnit’s inputs.
    The same arguments that are passed to AudioUnitRenderSlice() are passed on to
    the callback here.

    Code:
    typedef CALLBACK_API_C( OSStatus, AudioUnitRenderCallback ) 
                                                      (void *inRefCon, AudioUnitRenderActionFlags 
                                                      inActionFlags, const AudioTimeStamp *inTimeStamp, 
                                                      UInt32 inBusNumber, AudioBuffer *ioData);
    The following callback is called when a client registers for notifications of
    property changes to an AudioUnit with the AudioUnitAddPropertyListener
    call.
    Code:
    typedef CALLBACK_API_C( void, AudioUnitPropertyListenerProc ) 
                                                      (void *inRefCon, AudioUnit ci, 
                                                      AudioUnitPropertyID inID, AudioUnitScope inScope, 
                                                      AudioUnitElement inElement);
    Last edited by Subsonics; 01-27-2009 at 06:58 PM.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can always look things up in Wikipedia (don't forget to donate).

    In standard C library, for example, you need to provide a comparison function for qsort, that will be "called back" from the qsort function.

    Or you can register a function that will be called when the program exits.

    etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In programming, a callback is a function in your code that gets called from a function within the API functionality you are using.

    A simple example of that is the compare function that you need to provide for the standard function qsort(). It is called every time the qsort function needs to compare two data items (to make qsort work on any type of element, it can't know how to compare strings, integers, complex numbers or telephone numbers, so we have to supply a function pointer to the qsort function).

    Often in GUI programming, functions take a function parameter to a call, and later on when something the API relates to happens (e.g. a mouse-click, mouse cursor entering a region on the screen) your function gets called.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So basicaly a function that I make but dont call directly, but instead pass as a pointer in the argument field of another function, that will call it on specific events. Hmm I think I have to take one step back and do some simple experiments on this to get it to stick. Thanks for the explanation.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by Subsonics View Post
    So basicaly a function that I make but dont call directly, but instead pass as a pointer in the argument field of another function, that will call it on specific events. Hmm I think I have to take one step back and do some simple experiments on this to get it to stick. Thanks for the explanation.
    Example:

    Code:
    void print_brewbuck()
    {
        printf("Brewbuck");
    }
    
    void print_two_names()
    {
        static int printed_first = 0;
    
        if(printed_first == 0)
            printf("Brewbuck");
        else
            printf("Subsonics");
        printed_first = 1;
    }
    
    void print_greeting_message(void (*name_func)())
    {
        printf("Hello ");
        name_func();
        printf("! How are you today?\n");
        printf("Thanks for stopping by, ");
        name_func();
        printf("\n");
    }
    
    int main()
    {
        print_greeting_message(print_brewbuck);
        print_greeting_message(print_two_names);
        return 0;
    }
    In this case, print_greeting_message() is a function which takes a callback as a parameter. I wrote two callbacks, print_brewbuck() and print_two_names().

    The idea is that the callback can alter the behavior of a piece of code it doesn't directly control. This could be because of app/library separation, or to reuse code, or just to provide flexibility. This is a silly example -- in reality, the callback probably takes at least one parameter (a pointer to some context, which I've simulated here with a static variable).

    A callback could be used for event handling, doing something when another function enters a particular state, feeding input or writing output, performing the major processing in the core of a generic looping algorithm, etc.

    EDIT: There was a bug in my code, which I fixed with the line highlighted in red. It wasn't printing a final newline. That's actually demonstrative, because it shows how I only had to change the code in a single place to fix two different messages, because of the abstraction provided by the callback.
    Last edited by brewbuck; 01-27-2009 at 08:09 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Wow, thank you brewbuck. Thats a great explanation that answered both the how and why.

    Greetings :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 03:27 AM
  2. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 03:24 AM
  3. Visual Basic Question?
    By ob1cnobe in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 10:31 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21