Callback (computer science)

Callback (computer science)

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (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 for code reuse.


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).


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:

int i; for (i = 0; i < length; i++) { if (array [i] > 5) { break; } } if (i < length) { printf("Item %d ", i); } else { printf("Not found "); }

Next, the callback approach:

/* LIBRARY CODE */ int traverseWith(int array [] , size_t length, int (*callback)(int index, int item, void *param), void *param) { int exitCode = 0; for (int i = 0; i < length; i++) { exitCode = callback(i, array [i] , param); if (exitCode != 0) { break; } } return exitCode; } /* APPLICATION CODE */ int search (int index, int item, void *param) { if (item > 5) { *(int *)param = index; return 1; } else { return 0; } } /* (in another function) */ int index; int found; found = traverseWith(array, length, &search, &index); if (found) { printf("Item %d ", index); } else { printf("Not found "); }

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:

; LIBRARY CODE (defun traverseWith (array callback) (let ((exitCode nil) (i 0)) (while (and (not exitCode) (< i (length array))) (setf exitCode (funcall callback i (aref array i))) (setf i (+ i 1))) exitCode)) ; APPLICATION CODE (let (index found) (setf found (traverseWith array (lambda (idx item) (if (<= item 5) nil (progn (setf index idx) t))))))

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.


The form of a callback varies among programming languages.

*C and C++ allow function pointers as arguments to other functions.
*Several programming languages (though especially functional programming languages such as Scheme or ML) allow closures, a generalization of function pointers, as arguments to other functions.
*Several programming languages, especially interpreted languages, allow one to pass the "name" of a function A as a parameter to a function B and have B call A by means of eval.
*In object-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. The Standard 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 manage function pointers. These can be used for callback operations.
*Perl supports subroutine references. [cite web |url= |title=Perl Cookbook - 11.4. Taking References to Functions|accessdate=2008-03-03] [cite web |url= |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 handlers in an operating system respond to hardware conditions, signal handlers of a process are triggered by the operating system, and event handlers 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 the Observer 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 or Qt's signals and slots).

ee also

*Signals and slots
*libsigc++, a callback library for C++
*Implicit invocation
*User exit
*Inversion of control

External links

* [ Style Case Study #2: Generic Callbacks]
* [ C++ Callback Solution]
* [ Basic Instincts: Implementing Callback Notifications Using Delegates]
* [ Implement Script Callback Framework in ASP.NET]
* [ Callback Balance C++ net framework]
* [ Callback Procedures]
* [ Implement callback routines in Java]


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Closure (computer science) — In computer science, a closure (also lexical closure, function closure, function value or functional value) is a function together with a referencing environment for the non local variables of that function.[1] A closure allows a function to… …   Wikipedia

  • Callback — can mean: * Callback (computer science), executable code that is passed as a parameter to other code * Callback (telecommunications), the telecommunications event that occurs when the originator of a call is immediately called back in a second… …   Wikipedia

  • Reflection (computer programming) — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computi …   Wikipedia

  • Event (computing) — In computing an event is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program. Typically events are handled synchronous with the program flow, that is, the program has one or …   Wikipedia

  • Event handler — In computer programming, an event handler is an asynchronous callback subroutine that handles inputs received in a program. Each event is a piece of application level information from the underlying framework, typically the GUI toolkit. GUI… …   Wikipedia

  • Active Object — Активный объект (active object)  это шаблон проектирования, который отделяет поток выполения метода от потока, в котором он был вызван.[1] Целью данного шаблона является предоставление параллельности выполнения используя асинхронные вызовы… …   Википедия

  • Continuation-passing style — In functional programming, continuation passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. Gerald Jay Sussman and Guy L. Steele, Jr. coined the phrase in AI Memo 349 (1975), which… …   Wikipedia

  • Inversion of control — In software engineering, Inversion of Control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming. In traditional… …   Wikipedia

  • Patron de conception — Pour les articles homonymes, voir Patron. le patron Proxy En informatique, et plus particulièrement en développement logiciel, un patron de conception (en anglais  …   Wikipédia en Français

  • Hooking — For the slang term meaning female prostitution, see prostitution. For the ice hockey penalty, see hooking (ice hockey). Hooking in programming is a technique employing so called hooks to make a chain of procedures as an event handler. Thus, after …   Wikipedia