In the Learning OpenCV
book, I came to the term callback
, and sometimes used with routine
as callback routine
.
What d
Following the Holywood principle "Don't call us, we call you", a callback is a reference to a function which is passed to another function.
The callback will be called by the function it is given to for instance when data is available or certain processing steps need to be performed.
"Routine" in this context is the same as "function". The term goes back to older languages (like Fortran) that made a difference between functions, that returns values, and (sub)routines that don't.
"Callback" is a technique where you provide a pointer to one of your functions ("routines") to the system/API/framework and the system/API/framework would call it back when it feels like doing so. So a callback routine, or simply a callback, is a function that's intended for such usage.
In strictly object languages (like Java) they typically use listeners and delegates for that. The callback technique, in its C++ form, has the advantage that's it's compatible with non-object-oriented languages like classic C.
EDIT: in the Microsoft C run-time library, this technique is used for qsort() function. The compare
argument is a function pointer to a callback routine. It's called by the RTL whenever two array elements need to be compared. It's not a typical example 'cause all the calls to compare
happen before the qsort() call returns.
In Win32 API, callbacks are a staple. The window procedure is a prime example - you pass a pointer to it in the WNDCLASS structure, the system calls the procedure back as the message arrive. In this case, the callback routine is invoked long after the RegisterClass() - for the whole lifetime of the window.
In POSIX/Unix/Linux, the signal processing function is an example. See the signal() syscall description.
"I don't call it by myself, but the system (or some others) will call it". That's callback.
Callback functions are function which are not called explicitly such functions automatically invoked after some event occurs, for example after pressing "ctrl+c" SIGINT signal generated so automatically handler will execute.
They mean that you pass a pointer to a procedure to OpenCV
. This will be called back
when something happens. This can e.g. seen at cvSetMouseCallback(). The function referenced by the pointer will be called whenever the mouse moves.
What is a Callback function?
In simple terms, a Callback function is a function that is not called explicitly by the programmer. Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.
This mechanism is typically used when an operation(function) takes a long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation. Typically, Callback functions help implement such an asynchronous mechanism, wherein the caller registers to get inimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.
A practical example:
Windows event processing:
virtually all windows programs set up an event loop, that makes the program respond to particular events (e.g button presses, selecting a check box, window getting focus) by calling a function. The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.
A source Code Illustration:
//warning: Mind compiled code, intended to illustrate the mechanism
#include <map>
typedef void (*Callback)();
std::map<int, Callback> callback_map;
void RegisterCallback(int event, Callback function)
{
callback_map[event] = function;
}
bool finished = false;
int GetNextEvent()
{
static int i = 0;
++i;
if (i == 5) finished = false;
}
void EventProcessor()
{
int event;
while (!finished)
{
event = GetNextEvent();
std::map<int, Callback>::const_iterator it = callback_map.find(event);
if (it != callback_map.end()) // if a callback is registered for event
{
Callback function = *it;
if (function)
{
(*function)();
}
else
{
std::cout << "No callback found\n";
}
}
}
}
void Cat()
{
std::cout << "Cat\n";
}
void Dog()
{
std::cout << "Dog\n";
}
void Bird()
{
std::cout << "Bird\n";
}
int main()
{
RegisterCallBack(1, Cat);
RegisterCallback(2, Dog);
RegisterCallback(3, Cat);
RegisterCallback(4, Bird);
RegisterCallback(5, Cat);
EventProcessor();
return 0;
}
The above would output the following:
Cat
Dog
Cat
Bird
Cat
Hope this helps!
Note:
Imported this answer from one of my old answers here.