So I have started to look into some C API documentation and see this quite frequently, what is a callback exactly.
So I have started to look into some C API documentation and see this quite frequently, what is a callback exactly.
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?
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.
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:
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().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; }
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); //}
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.
The following callback is called when a client registers for notifications ofCode:typedef CALLBACK_API_C( OSStatus, AudioUnitRenderCallback ) (void *inRefCon, AudioUnitRenderActionFlags inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, AudioBuffer *ioData);
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.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.