Callback (computer programming)
A callback function (English callbackFunction ) refers to computer science, a function that is another function passed as a parameter and called by this under certain conditions. This approach follows the design pattern of Inversion of Control.
Callback in the same sequence
The picture shows a call to a routine in another context ( operating system call, another software module). This call is passed a reference to the callback routine. The called routine can then call the callback routine in their sequence. The callback routine ultimately stores data that can be used in the further execution upon return of the original call. In the English literature, this form is called a blocking or synchronous callback callback.
The advantage of this technique is that the access is formulated on the data in its own context and / or in the recall can be any programmatic preparation: The parameters and the call time to be determined in another software module, the determination is what carried out in the recall is for the own software module.
This makes it possible to define functions and generally only be determined when the function by specifying the callback function the behavior accurately. Frequently gets a callback function does not have a name, but is an anonymous function defined directly in the call (see also lambda calculus ). About callback functions to reach a loose coupling between individual components.
Recall from another thread
If you specify a callback function it is up to the called function when it executes the callback ( Inversion of Control ). It is possible that the recall is done at a later date from another thread out. The picture shows the principle. The assignment of the callback routine to its own context is the same as in the above scheme. Since the recall can be done in another thread, but the conditions for data consistency are also observed ( mutex ). In the English literature, this form is called a deferred callback or asynchronous callback.
An example of this is the call to SetTimer ( hWnd, ident, milliseconds, callback routine ) in the Windows API. The call SetTimer ( ... ) is a short operating system call initializes the timer only in the operating system. The callback routine is only called when the time has expired. This happens regardless of the sequence in a thread of the operating system after the call to SetTimer (...). In the callback routine to be provided can be run arbitrary code. However, it is to note that the callback routine may only be short and non-blocking. Otherwise there is a risk that the entire operating system, or at least the current process is blocked. Ultimately, the callback routine should really take only data and not involve complex actions themselves. In particular, can be created and stored for an event-oriented programming, an event instance that controls other processes.
Durable callback routine
A callback routine can only apply each of an invocation. The callback routine can be called several times in reference to the call to the associated action is completed, so temporary.
But the permanent assignment of a routine is sometimes called callback routine, see for example msdn.microsoft ... ThreadProc callbackFunction: When creating a thread using the Windows API, the thread function is specified as a callback function.
The assignment of an event handler, for example, to an HTML element or a widget within a programming graphical user interfaces follows the same pattern, but is usually referred to in this context as a callback, but as an event handler.
Simple callback function or object-oriented callback
The so-called callback function in the Windows API are each simple C functions. Windows API is provided as a C- port. In forums, there are often questions about the possibility of C functions to assign. Necessary here is an encapsulation of the C call in a C function that knows the data instance.
The situation is different in object-oriented approaches a recall. Instead of specifying a function pointer, the reference is passed to a data object for the callback. The data object must implement a specific interface for the callback. The interface will contain several possible callback functions in the table of virtual methods within the data instance and at the same time knows the necessary data for the execution of the callback. The basic scheme is exactly the same as shown in the above schemes.
Example of the organization of a simple callback in the same sequence
It is to be written a function apply_to applying any other function on a list of values and returns a list of results. An implementation in pseudo code:
Apply_to function ( callback function, values): var result; / / List of results foreach w in values do / / for all values ... E = recall function (W ); / / Call the callback function ... result [ ] = e; / / And append the result to the result. end; return result; end apply_to; This function can now be used as follows:
Double function (value): / / this is a callback function return value * 2; double end; function square (value): / / this is a different callback function return value * value; square end; / / Apply the functions on a list: value = (1, 2, 3, 4); doppelte_Werte = apply_to ( double, values); / / results in (2, 4, 6, 8 ) quadrierte_Werte = apply_to ( square, values); / / results in (1, 4, 9, 16 ) Or, shorter, in lambda notation:
Value = (1, 2, 3, 4); doppelte_Werte apply_to = (lambda x: x * 2, values); / / results in (2, 4, 6, 8 ) quadrierte_Werte apply_to = (lambda x: x * x, values); / / results in (1, 4, 9, 16 ) see also
- Hook ( computer science )
- Signal - slot concept