Callback (computer programming)

A function that accepts a callback parameter may be designed to call back before returning to its caller which is known as synchronous or blocking.

Programming languages support callbacks in different ways such as function pointers, lambda expressions and blocks.

The customer gives to a clerk a list of items, a check to cover their cost and delivery instructions.

The application supplies a reference to a custom callback function for the windowing system to call.

The windowing system calls this function to notify the application of events like mouse clicks and key presses.

Using the same storage allows different languages to directly share callbacks without a design-time or runtime interoperability layer.

Many dynamic languages, such as JavaScript, Lua, Python, Perl[1][2] and PHP, allow a function object to be passed.

In an object-oriented programming language such as Java versions before function-valued arguments, the behavior of a callback can be achieved by passing an object that implements an interface.

[5] Callbacks have a wide variety of uses, for example in error signaling: a Unix program might not want to terminate immediately when it receives SIGTERM, so to make sure that its termination is handled properly, it would register the cleanup function as a callback.

Callbacks may also be used to control whether a function acts or not: Xlib allows custom predicates to be specified to determine whether a program wishes to handle an event.

In the following Kotlin code, function askAndAnswer uses parameter getAnswer as a blocking callback.

In the following JavaScript code, function calculate uses parameter operate as a blocking callback.

For example: Deferred callbacks are commonly used for handling events from the user, the client and timers.

In the following Julia code, function calculate accepts a parameter operate that is used as a blocking callback.

A callback is often back on the level of the original caller.