- Callback (computer science)
In
computer programming , a callback isexecutable code that is passed as an argument to other code. It allows a lower-level software layer to call asubroutine (or function) defined in a higher-level layer.Usually, the higher-level code starts by calling a function within the lower-level code, passing to it a
pointer or handle to another function. While the lower-level function executes, it may call the passed-in function any number of times to perform some subtask. In another scenario, the lower-level function registers the passed-in function as a "handler" that is to be called asynchronously by the lower-level at a later time in reaction to something.A callback can be used as a simpler alternative to polymorphism and
generic programming , in that the exact behavior of a function can be dynamically determined by passing different (yet compatible) function pointers or handles to the lower-level function. This can be a very powerful technique forcode reuse .Motivation
To understand the motivation for using callbacks, consider the problem of performing an arbitrary operation on each item in a list. One approach is to iterate over the list, operating on each object. This is the most common solution in practice, but it is not ideal; the code to manage the iterator (for example, a
for
statement) must be duplicated at each point in the code where the list is traversed. Furthermore, if the list is updated by an asynchronous process (for example, if an item is added or removed), the iterator might skip over items or become corrupt during the traversal.An alternative might be to create a new library function that performs the desired operation with appropriate synchronization. This approach still requires each new library function to contain the code to traverse the list. This solution is not acceptable for generic libraries intended for various applications; the library developer cannot anticipate every application need, and the application developer should not need to know the details of the library implementation.
Callbacks solve these shortcomings. One procedure is written to traverse the list, and this procedure uses application-provided code to operate on each item. There is a clear distinction between the library and the application without sacrificing flexibility.
A callback may also be regarded as a form of runtime binding, as discussed in the influential
Design Patterns book (see the Chapter 1 summary).Example
The following code in C demonstrates the use of callbacks for the specific case of searching an array for an item (in this case, the first integer greater than 5). First, the iteration approach:
Next, the callback approach:
Note that traverseWith receives an extra parameter that the callback can use for its own purposes. Normally a callback uses such a parameter as a pointer to application data outside its own scope (in this case, the variable that receives the index). This feature is necessary only in a statically scoped language such as C or C++ (in C++ and OO languages other solutions are however possible, see below). Lexically scoped languages supporting closures (including functional programming languages) can provide access to application data automatically by callbacks referring to local variables. In Lisp, for example, the same program would look like this:
The callback function is now defined at the point of use, and it refers to "index" by name. Synchronization concerns have been omitted from these examples, but they can easily be addressed in the traverseWith function. More importantly, they can be addressed, or ignored, by changing only that function.
Implementation
The form of a callback varies among
programming language s.*C and
C++ allowfunction pointer s as arguments to other functions.
*Several programming languages (though especiallyfunctional programming languages such as Scheme or ML) allow closures, a generalization of function pointers, as arguments to other functions.
*Several programming languages, especiallyinterpreted language s, allow one to pass the "name" of a function A as a parameter to a function B and have B call A by means ofeval .
*Inobject-oriented programming languages, a call can accept an object that implements some abstract interface, without specifying in detail how the object should do so. The programmer who implements that object may use the interface's methods exclusively for application-specific code. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns like Visitor, Observer, and Strategy.
*C++ allows objects to provide their own implementation of the function call operation. TheStandard Template Library accepts these objects (called "functors"), as well as function pointers, as parameters to various polymorphic algorithms
*C# .NET Framework provides a type-safe encapsulating reference, a 'delegate', to managefunction pointer s. These can be used for callback operations.
*Perl supports subroutine references. [cite web |url=http://www.unix.org.ua/orelly/perl/cookbook/ch11_05.htm |title=Perl Cookbook - 11.4. Taking References to Functions|accessdate=2008-03-03] [cite web |url=http://www.unix.org.ua/orelly/perl/advprog/ch04_02.htm |title=Advanced Perl Programming - 4.2 Using Subroutine References |accessdate=2008-03-03]
*Some systems have built-in programming languages to support extension and adaptation. These languages provide callbacks without the need for separate software development tools.pecial cases
Callback functions are also frequently used as a means to handle exceptions arising within the low level function, as a way to enable side-effects in response to some condition,or as a way to gather operational statistics in the course of a larger computation.
Interrupt handler s in anoperating system respond to hardware conditions,signal handler s of a process are triggered by the operating system, andevent handler s process the asynchronous input a program receives.A pure callback function is one which is
purely functional (always returns the same value given the same inputs) and free of observable side-effects. Some uses of callbacks, such as the sorting example, require pure callback functions to operate correctly.A special case of a callback is called a predicate callback, or just predicate for short. This is a pure callback function which accepts a single input value and returns a
Boolean value. These types of callbacks are useful for filtering collections of values by some condition.In
event-driven programming , there is often use, in different forms, of theObserver pattern , which essentially allows the use of multicast callbacks, and where callbacks are "registered" early, to be invoked at callee's discretion (i.e. when a given event occurs).Some programming languages also have direct support for this construct (for instance .NET delegates orQt 'ssignals and slots ).ee also
*
Signals and slots
*libsigc++ , a callback library for C++
*Implicit invocation
*User exit
*Inversion of control External links
* [http://gotw.ca/gotw/083.htm Style Case Study #2: Generic Callbacks]
* [http://partow.net/programming/templatecallback/index.html C++ Callback Solution]
* [http://msdn.microsoft.com/msdnmag/issues/02/12/basicinstincts Basic Instincts: Implementing Callback Notifications Using Delegates]
* [http://www.codeproject.com/aspnet/ScriptCallbackFramework.asp Implement Script Callback Framework in ASP.NET]
* [http://www.stargeek.com/item/91792.html Callback Balance C++ net framework]
* [http://www.itspecial.org/1cd_dlg.htm#Callback Callback Procedures]
* [http://www.javaworld.com/javaworld/javatips/jw-javatip10.html Implement callback routines in Java]References
Wikimedia Foundation. 2010.