Template:Short description Template:For Template:More references
In computer programming, a callback is a function that is stored as data (a reference) and designed to be called by another function Template:Endash often back to the original abstraction layer.
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. The function that accepts a callback may be designed to store the callback so that it can be called back after returning which is known as asynchronous, non-blocking or deferred.
Programming languages support callbacks in different ways such as function pointers, lambda expressions and blocks.
A callback can be likened to leaving instructions with a tailor for what to do when a suit is ready, such as calling a specific phone number or delivering it to a given address. These instructions represent a callback: a function provided in advance to be executed later, often by a different part of the system and not necessarily by the one that received it.
The term callback can be misleading, as it does not necessarily imply a return to the original caller, unlike a telephone callback.
Mesa programming language formalised the callback mechanism used in Programming Languages. By passing a procedure as a parameter, Mesa essentially delegated the execution of that procedure to a later point in time when a specific event occurred, similar to how callbacks are implemented in modern programming languages.<ref>Template:Cite journal</ref>
Template:AnchorUseEdit
A blocking callback runs in the execution context of the function that passes the callback. A deferred callback can run in a different context such as during interrupt or from a thread. As such, a deferred callback can be used for synchronization and delegating work to another thread.
Event handlingEdit
A callback can be used for event handling. Often, consuming code registers a callback for a particular type of event. When that event occurs, the callback is called.
Callbacks are often used to program the graphical user interface (GUI) of a program that runs in a windowing system. 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.
Asynchronous actionEdit
A callback can be used to implement asynchronous processing. A caller requests an action and provides a callback to be called when the action completes which might be long after the request is made.
PolymorphismEdit
A callback can be used to implement polymorphism. In the following pseudocode, <syntaxhighlight lang="text" class="" style="" inline="1">say_hi</syntaxhighlight> can take either <syntaxhighlight lang="text" class="" style="" inline="1">write_status</syntaxhighlight> or <syntaxhighlight lang="text" class="" style="" inline="1">write_error</syntaxhighlight>.
<syntaxhighlight lang="python"> def write_status(message: str):
write(stdout, message)
def write_error(message: str):
write(stderr, message)
def say_hi(write):
write("Hello world")
</syntaxhighlight>
ImplementationEdit
The callback technology is implemented differently by programming language.
In assembly, C, C++, Pascal, Modula2 and other languages, a callback function is stored internally as a function pointer. Using the same storage allows different languages to directly share callbacks without a design-time or runtime interoperability layer. For example, the Windows API is accessible via multiple languages, compilers and assemblers.
C++ also allows objects to provide an implementation of the function call operation. The Standard Template Library accepts these objects (called functors) as parameters.
Many dynamic languages, such as JavaScript, Lua, Python, Perl<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and PHP, allow a function object to be passed.
CLI languages such as C# and VB.NET provide a type-safe encapsulating function reference known as delegate.
Events and event handlers, as used in .NET languages, provide 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.
Many languages, including Perl, Python, Ruby, Smalltalk, C++ (11+), C# and VB.NET (new versions) and most functional languages, support lambda expressions, unnamed functions with inline syntax, that generally acts as callbacks..
In some languages, including Scheme, ML, JavaScript, Perl, Python, Smalltalk, PHP (since 5.3.0),<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> C++ (11+), Java (since 8),<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and many others, a lambda can be a closure, i.e. can access variables locally defined in the context in which the lambda is defined.
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. The methods of this object are callbacks.
In PL/I and ALGOL 60 a callback procedure may need to be able to access local variables in containing blocks, so it is called through an entry variable containing both the entry point and context information. <ref>Template:Cite book</ref>
Example codeEdit
CEdit
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 C code, function print_number
uses parameter get_number
as a blocking callback. print_number
is called with get_answer_to_most_important_question
which acts as a callback function. When run the output is: "Value: 42".
<syntaxhighlight lang="c">
- include <stdio.h>
- include <stdlib.h>
void print_number(int (*get_number)(void)) {
int val = get_number(); printf("Value: %d\n", val);
}
int get_answer_to_most_important_question(void) {
return 42;
}
int main(void) {
print_number(get_answer_to_most_important_question); return 0;
} </syntaxhighlight>
C++Edit
In C++, functor can be used in addition to function pointer.
C#Edit
In the following C# code,
method Helper.Method
uses parameter callback
as a blocking callback. Helper.Method
is called with Log
which acts as a callback function. When run, the following is written to the console: "Callback was: Hello world".
<syntaxhighlight lang="c#"> public class MainClass {
static void Main(string[] args) { Helper helper = new Helper(); helper.Method(Log); }
static void Log(string str) { Console.WriteLine($"Callback was: {str}"); }
}
public class Helper {
public void Method(Action<string> callback) { callback("Hello world"); }
} </syntaxhighlight>
KotlinEdit
In the following Kotlin code, function askAndAnswer
uses parameter getAnswer
as a blocking callback. askAndAnswer
is called with getAnswerToMostImportantQuestion
which acts as a callback function. Running this will tell the user that the answer to their question is "42".
<syntaxhighlight lang="kotlin"> fun main() {
print("Enter the most important question: ") val question = readLine() askAndAnswer(question, ::getAnswerToMostImportantQuestion)
}
fun getAnswerToMostImportantQuestion(): Int {
return 42
}
fun askAndAnswer(question: String?, getAnswer: () -> Int) {
println("Question: $question") println("Answer: ${getAnswer()}")
} </syntaxhighlight>
JavaScriptEdit
In the following JavaScript code, function calculate
uses parameter operate
as a blocking callback. calculate
is called with multiply
and then with sum
which act as callback functions.
<syntaxhighlight lang="javascript"> function calculate(a, b, operate) {
return operate(a, b);
} function multiply(a, b) {
return a * b;
} function sum(a, b) {
return a + b;
} // outputs 20 alert(calculate(10, 2, multiply)); // outputs 12 alert(calculate(10, 2, sum)); </syntaxhighlight>
The collection method <syntaxhighlight lang="text" class="" style="" inline="1">.each()</syntaxhighlight> of the jQuery library uses the function passed to it as a blocking callback. It calls the callback for each item of the collection. For example:
<syntaxhighlight lang="javascript"> $("li").each(function(index) {
console.log(index + ": " + $(this).text());
}); </syntaxhighlight>
Deferred callbacks are commonly used for handling events from the user, the client and timers. Examples can be found in <syntaxhighlight lang="text" class="" style="" inline="1">addEventListener</syntaxhighlight>, Ajax and XMLHttpRequest
.
<ref>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref>
In addition to using callbacks in JavaScript source code, C functions that take a function are supported via js-ctypes.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Red and REBOLEdit
The following REBOL/Red code demonstrates callback use.
- As alert requires a string, form produces a string from the result of calculate
- The get-word! values (i.e., :calc-product and :calc-sum) trigger the interpreter to return the code of the function rather than evaluate with the function.
- The datatype! references in a block! [float! integer!] restrict the type of values passed as arguments.
<syntaxhighlight lang="red"> Red [Title: "Callback example"]
calculate: func [
num1 [number!] num2 [number!] callback-function [function!]
][
callback-function num1 num2
]
calc-product: func [
num1 [number!] num2 [number!]
][
num1 * num2
]
calc-sum: func [
num1 [number!] num2 [number!]
][
num1 + num2
]
- alerts 75, the product of 5 and 15
alert form calculate 5 15 :calc-product
- alerts 20, the sum of 5 and 15
alert form calculate 5 15 :calc-sum </syntaxhighlight>
RustEdit
Rust have the <syntaxhighlight lang="text" class="" style="" inline="1">Fn</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">FnMut</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">FnOnce</syntaxhighlight> traits.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
<syntaxhighlight lang="rust"> fn call_with_one<F>(func: F) -> usize
where F: Fn(usize) -> usize { func(1)
}
let double = |x| x * 2; assert_eq!(call_with_one(double), 2); </syntaxhighlight>
LuaEdit
In this Lua code, function <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> accepts the <syntaxhighlight lang="text" class="" style="" inline="1">operation</syntaxhighlight> parameter which is used as a blocking callback. <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> is called with both <syntaxhighlight lang="text" class="" style="" inline="1">add</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">multiply</syntaxhighlight>, and then uses an anonymous function to divide.
<syntaxhighlight lang="lua">function calculate(a, b, operation)
return operation(a, b)
end
function multiply(a, b)
return a * b
end
function add(a, b)
return a + b
end
print(calculate(10, 20, multiply)) -- outputs 200 print(calculate(10, 20, add)) -- outputs 30 -- an example of a callback using an anonymous function print(calculate(10, 20, function(a, b)
return a / b -- outputs 0.5
end))</syntaxhighlight>
PythonEdit
In the following Python code, function <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> accepts a parameter <syntaxhighlight lang="text" class="" style="" inline="1">operate</syntaxhighlight> that is used as a blocking callback. <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> is called with <syntaxhighlight lang="text" class="" style="" inline="1">square</syntaxhighlight> which acts as a callback function.
<syntaxhighlight lang="python"> def square(val):
return val ** 2
def calculate(operate, val):
return operate(val)
calculate(square, 5) # outputs: 25 </syntaxhighlight>
JuliaEdit
In the following Julia code, function <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> accepts a parameter <syntaxhighlight lang="text" class="" style="" inline="1">operate</syntaxhighlight> that is used as a blocking callback. <syntaxhighlight lang="text" class="" style="" inline="1">calculate</syntaxhighlight> is called with <syntaxhighlight lang="text" class="" style="" inline="1">square</syntaxhighlight> which acts as a callback function.
<syntaxhighlight lang="jlcon"> julia> square(val) = val^2 square (generic function with 1 method) julia> calculate(operate, val) = operate(val) calculate (generic function with 1 method) julia> calculate(square, 5) 25 </syntaxhighlight>
See alsoEdit
- Command pattern
- Continuation-passing style
- Event loop
- Event-driven programming
- Implicit invocation
- Inversion of control
- libsigc++, a callback library for C++
- Signals and slots
- User exit
ReferencesEdit
External linksEdit
- Basic Instincts: Implementing Callback Notifications Using Delegates - MSDN Magazine, December 2002
- Implement callback routines in Java
- Implement Script Callback Framework in ASP.NET 1.x - Code Project, 2 August 2004
- Interfacing C++ member functions with C libraries (archived from the original on July 6, 2011)
- Style Case Study #2: Generic Callbacks