Callback (computer programming)

In computer programming, a callback, also known as a “call-after[1]function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later point in time as in an asynchronous callback. Programming languages support callbacks in different ways, often implementing them with subroutines, lambda expressions, blocks, or function pointers.

A function provided as an argument to another function to be called during its execution
For a discussion of callback with computer modems, see Callback (telecommunications).
This article needs additional citations for verification. (September 2015)
A callback is often back on the level of the original caller.

. . . Callback (computer programming) . . .

There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks). While blocking callbacks are invoked before a function returns (in the C example below, which illustrates a blocking callback, it is function

main), deferred callbacks may be invoked after a function returns. Deferred callbacks are often used in the context of I/O operations or event handling, and are called by interrupts or by a different thread in case of multiple threads. Due to their nature, blocking callbacks can work without interrupts or multiple threads, meaning that blocking callbacks are not commonly used for synchronization or delegating work to another thread.

Callbacks are used to program applications in windowing systems. In this case, the application supplies (a reference to) a specific custom callback function for the operating system to call, which then calls this application-specific function in response to events like mouse clicks or key presses. A major concern here is the management of privilege and security: while the function is called from the operating system, it should not run with the same privilege as the system. A solution to this problem is using rings of protection.

The form of a callback varies among programming languages:

  • In assembly, C, C++, Pascal, Modula2 and similar languages, a machine-level pointer to a function may be passed as an argument to another (internal or external) function. This is supported by most compilers and provides the advantage of using different languages together without special wrapper libraries or classes. One example may be the Windows API that is directly (more or less) accessible by many different languages, compilers and assemblers.
  • 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.
  • Many dynamic languages, such as JavaScript, Lua, Python, Perl[2][3] and PHP, simply allow a function object to be passed through.
  • CLI languages such as C# and VB.NET provide a type-safe encapsulating reference, a “delegate“, to define well-typed function pointers. These can be used as callbacks.
  • Events and event handlers, as used in .NET languages, provide generalized syntax for callbacks.
  • Functional languages generally support first-class functions, which can be passed as callbacks to other functions, stored as data or returned from functions.
  • Some languages, such as Algol 68, Perl, Python, Ruby, Smalltalk, C++11 and later, newer versions of C# and VB.NET as well as most functional languages, allow unnamed blocks of code (lambda expressions) to be supplied instead of references to functions defined elsewhere.
  • In some languages, e.g. Scheme, ML, JavaScript, Perl, Python, Smalltalk, PHP (since 5.3.0),[4] C++11 and later, Java (since 8),[5] and many others, such functions can be closures, i.e. they can access and modify variables locally defined in the context in which the function was defined. Note that Java cannot, however, modify the local variables in the enclosing scope.
  • In object-oriented programming languages without function-valued arguments, such as in Java before its 8 version, callbacks can be simulated by passing an instance of an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate[clarification needed]. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy.

. . . Callback (computer programming) . . .

This article is issued from web site Wikipedia. The original article may be a bit shortened or modified. Some links may have been modified. The text is licensed under “Creative Commons – Attribution – Sharealike” [1] and some of the text can also be licensed under the terms of the “GNU Free Documentation License” [2]. Additional terms may apply for the media files. By using this site, you agree to our Legal pages . Web links: [1] [2]

. . . Callback (computer programming) . . .

Previous post Nizhny Tagil
Next post Wildcoast