<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Callback_%28computer_programming%29</id>
	<title>Callback (computer programming) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Callback_%28computer_programming%29"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Callback_(computer_programming)&amp;action=history"/>
	<updated>2026-06-25T10:22:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Callback_(computer_programming)&amp;diff=387743&amp;oldid=prev</id>
		<title>imported&gt;LizardJr8: Reverted 1 edit by ~2025-33430-49 (talk) to last revision by Seaflour</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Callback_(computer_programming)&amp;diff=387743&amp;oldid=prev"/>
		<updated>2025-11-13T21:56:12Z</updated>

		<summary type="html">&lt;p&gt;Reverted 1 edit by &lt;a href=&quot;/index.php/Special:Contributions/~2025-33430-49&quot; title=&quot;Special:Contributions/~2025-33430-49&quot;&gt;~2025-33430-49&lt;/a&gt; (&lt;a href=&quot;/index.php?title=User_talk:~2025-33430-49&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:~2025-33430-49 (page does not exist)&quot;&gt;talk&lt;/a&gt;) to last revision by Seaflour&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Function reference passed to and called by another function}}&lt;br /&gt;
{{For |a discussion of callback with computer modems |Callback (telecommunications)}}&lt;br /&gt;
{{More references |date=September 2015}}&lt;br /&gt;
[[File:Callback-notitle.svg|thumb|370px|A callback is often back on the level of the original caller.]]&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], a &amp;#039;&amp;#039;&amp;#039;callback&amp;#039;&amp;#039;&amp;#039; is [[programming pattern]] in which a [[Function (computer programming)|function]] [[reference (computer science)|reference]] is passed from one context (consumer) to another (provider) such that the provider can call the function. If the function accesses [[state (computer science)|state]] or [[function (computer programming)|functionality]] of the consumer, then the call is &amp;#039;&amp;#039;back&amp;#039;&amp;#039; to the consumer; backwards compared to the normal [[flow of control]] in which a consumer calls a provider.&lt;br /&gt;
&lt;br /&gt;
A function that accepts a callback [[Parameter (computer programming)|parameter]] may be designed to call back before [[Return statement |returning]] to its caller. But, more typically, a callback reference is stored by the provider so that it can call the function later; as &amp;#039;&amp;#039;deferred&amp;#039;&amp;#039;. If the provider invokes the callback on the same [[thread (computer programming)|thread]] as the consumer, then the call is &amp;#039;&amp;#039;blocking&amp;#039;&amp;#039;, a.k.a. &amp;#039;&amp;#039;[[Synchronization (computer science)|synchronous]]&amp;#039;&amp;#039;. If instead, the provider invokes the callback on a different thread, then the call is &amp;#039;&amp;#039;[[Non-blocking algorithm |non-blocking]]&amp;#039;&amp;#039;, a.k.a. &amp;#039;&amp;#039;asynchronous&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The difference between a general function reference and a callback can be subtle, and some use the terms interchangeably but distinction generally depends on programming intent. If the intent is like the [[callback (telecommunications)|telephone callback]] {{endash}} that the original [[called party]] communicates back to the original [[calling party |caller]] {{endash}} then it&amp;#039;s a callback.&lt;br /&gt;
&lt;br /&gt;
== {{Anchor |TYPES}}Use ==&lt;br /&gt;
&lt;br /&gt;
A blocking callback runs in the [[Execution (computing)|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 (computing)|thread]]. As such, a deferred callback can be used for synchronization and delegating work to another thread.&lt;br /&gt;
&lt;br /&gt;
=== Event handling ===&lt;br /&gt;
&lt;br /&gt;
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 [[computer mouse|mouse]] clicks and [[computer keyboard|key]] presses.&lt;br /&gt;
&lt;br /&gt;
=== Asynchronous action ===&lt;br /&gt;
&lt;br /&gt;
A callback can be used to implement asynchronous processing. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
A callback can be used to implement [[Polymorphism (computer science)|polymorphism]]. In the following pseudocode, {{code|say_hi}} can take either {{code|write_status}} or {{code|write_error}}.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from typing import Callable&lt;br /&gt;
&lt;br /&gt;
def write_status(message: str) -&amp;gt; None:&lt;br /&gt;
    write(stdout, message)&lt;br /&gt;
&lt;br /&gt;
def write_error(message: str) -&amp;gt; None:&lt;br /&gt;
    write(stderr, message)&lt;br /&gt;
&lt;br /&gt;
def say_hi(write: Callable[[str], None]) -&amp;gt; None:&lt;br /&gt;
    write(&amp;quot;Hello world&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
The callback technology is implemented differently by [[programming language]].&lt;br /&gt;
&lt;br /&gt;
In [[Assembly language|assembly]], [[C (programming language)|C]], [[C++]], [[Pascal (programming language)|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 [[program lifecycle phase|design-time or runtime]] [[interoperability]] [[Abstraction layer|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 &amp;#039;&amp;#039;[[function object|functors]]&amp;#039;&amp;#039;) as parameters. Many [[Dynamic programming language|dynamic languages]], such as [[JavaScript]], [[Lua (programming language)|Lua]], [[Python (programming language)|Python]], [[Perl]]&amp;lt;ref&amp;gt;{{cite web |url=http://www.unix.org.ua/orelly/perl/cookbook/ch11_05.htm |title=Perl Cookbook - 11.4. Taking References to Functions|date=2 July 1999 |accessdate=2008-03-03}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web |url=http://www.unix.org.ua/orelly/perl/advprog/ch04_02.htm |title=Advanced Perl Programming - 4.2 Using Subroutine References |date=2 July 1999 |accessdate=2008-03-03}}&amp;lt;/ref&amp;gt; and [[PHP]], allow a function object to be passed. [[List of CLI languages|CLI languages]] such as [[C Sharp (programming language)|C#]] and [[VB.NET]] provide a [[type safety|type-safe]] encapsulating function reference known as [[Delegate (CLI)|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.&lt;br /&gt;
&lt;br /&gt;
Many languages, including Perl, Python, [[Ruby (programming language)|Ruby]], [[Smalltalk]], [[C++]] (11+), C# and VB.NET (new versions) and most functional languages, support [[lambda (programming)|lambda expressions]], unnamed functions with inline syntax, that generally act as callbacks. In some languages, including [[Scheme (programming language)|Scheme]], [[ML (programming language)|ML]], JavaScript, Perl, Python, Smalltalk, PHP (since 5.3.0),&amp;lt;ref&amp;gt;{{cite web |url=https://secure.php.net/manual/en/functions.anonymous.php |title=PHP Language Reference - Anonymous functions | accessdate=2011-06-08}}&amp;lt;/ref&amp;gt; C++ (11+), Java (since 8),&amp;lt;ref&amp;gt;{{cite web |url=http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html|title=What&amp;#039;s New in JDK 8|work=oracle.com}}&amp;lt;/ref&amp;gt; and many others, a lambda can be a [[Closure (computer science)|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 (programming language)|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 &amp;#039;&amp;#039;entry variable&amp;#039;&amp;#039; containing both the entry point and context information.&amp;lt;ref&amp;gt;{{cite book |editor1-last=Belzer |editor1-first=Jack |editor2-last=Holzman |editor2-first=Albert G |editor3-last=Kent |editor3-first=Allen |title=Encyclopedia of Computer Science and Technology: Volume 12 |date=1979 |publisher=Marcel Dekker, inc. |isbn=0-8247-2262-0 |page=164 |url=https://books.google.com/books?id=IFmaqTI9-KsC&amp;amp;pg=PA164 |access-date=January 28, 2024}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example code==&lt;br /&gt;
&lt;br /&gt;
=== C ===&lt;br /&gt;
&lt;br /&gt;
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 (programming language)|C]] code, function &amp;lt;code&amp;gt;printNumber()&amp;lt;/code&amp;gt; uses parameter &amp;lt;code&amp;gt;get_number&amp;lt;/code&amp;gt; as a blocking callback. &amp;lt;code&amp;gt;printNumber()&amp;lt;/code&amp;gt; is called with &amp;lt;code&amp;gt;getAnswerToMostImportantQuestion()&amp;lt;/code&amp;gt; which acts as a callback function. When run the output is: &amp;quot;Value: 42&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void printNumber(int (*getNumber)(void)) {&lt;br /&gt;
    int val = getNumber();&lt;br /&gt;
    printf(&amp;quot;Value: %d\n&amp;quot;, val);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int getAnswerToMostImportantQuestion(void) {&lt;br /&gt;
    return 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    printNumber(getAnswerToMostImportantQuestion);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
In C++, [[Function object|functor]]s can be used in addition to function pointer. A functor is an object with &amp;lt;code&amp;gt;operator()&amp;lt;/code&amp;gt; defined. For example, the objects in &amp;lt;code&amp;gt;std::views&amp;lt;/code&amp;gt; are functors. This is an example of using functors in C++:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
class MyCallback {&lt;br /&gt;
public:&lt;br /&gt;
    void operator()(int x) {&lt;br /&gt;
        std::println(&amp;quot;Callback called with value: {}&amp;quot;, x);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
template &amp;lt;typename Callback&amp;gt;&lt;br /&gt;
void performOperation(int a, Callback callback) {&lt;br /&gt;
    std::println(&amp;quot;Performing operation on: {}&amp;quot;, a);&lt;br /&gt;
    callback(a);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    MyCallback callback;&lt;br /&gt;
    int value = 10;&lt;br /&gt;
    performOperation(value, callback);&lt;br /&gt;
    &lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;std::function&amp;lt;R(Args...)&amp;gt;&amp;lt;/code&amp;gt; is a type-erased wrapper for any callable objects, introduced in [[C++11]]:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
using std::function;&lt;br /&gt;
&lt;br /&gt;
void performOperation(int a, function&amp;lt;void(int)&amp;gt; callback) {&lt;br /&gt;
    std::println(&amp;quot;Performing operation on: {}&amp;quot;, a);&lt;br /&gt;
    callback(a);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    int value = 10;&lt;br /&gt;
    performOperation(value, [](int x) -&amp;gt; void {&lt;br /&gt;
        std::println(&amp;quot;Callback called with value: {}&amp;quot;, x);&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== C# ===&lt;br /&gt;
&lt;br /&gt;
In the following [[C Sharp (programming language)|C#]] code, &lt;br /&gt;
method &amp;lt;code&amp;gt;Helper.Method&amp;lt;/code&amp;gt; uses parameter &amp;lt;code&amp;gt;callback&amp;lt;/code&amp;gt; as a blocking callback. &amp;lt;code&amp;gt;Helper.Method&amp;lt;/code&amp;gt; is called with &amp;lt;code&amp;gt;Log&amp;lt;/code&amp;gt; which acts as a callback function. When run, the following is written to the console: &amp;quot;Callback was: Hello world&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c#&amp;quot;&amp;gt;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        Helper helper = new Helper();&lt;br /&gt;
        helper.Method(Log);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static void Log(string str)&lt;br /&gt;
    {&lt;br /&gt;
        Console.WriteLine($&amp;quot;Callback was: {str}&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Helper&lt;br /&gt;
{&lt;br /&gt;
    public void Method(Action&amp;lt;string&amp;gt; callback)&lt;br /&gt;
    {&lt;br /&gt;
        callback(&amp;quot;Hello world&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kotlin ===&lt;br /&gt;
&lt;br /&gt;
In the following [[Kotlin (programming language)|Kotlin]] code, function &amp;lt;code&amp;gt;askAndAnswer&amp;lt;/code&amp;gt; uses parameter &amp;lt;code&amp;gt;getAnswer&amp;lt;/code&amp;gt; as a blocking callback. &amp;lt;code&amp;gt;askAndAnswer&amp;lt;/code&amp;gt; is called with &amp;lt;code&amp;gt;getAnswerToMostImportantQuestion&amp;lt;/code&amp;gt; which acts as a callback function. Running this will tell the user that the answer to their question is &amp;quot;42&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;kotlin&amp;quot;&amp;gt;&lt;br /&gt;
fun main() {&lt;br /&gt;
    print(&amp;quot;Enter the most important question: &amp;quot;)&lt;br /&gt;
    val question = readLine()&lt;br /&gt;
    askAndAnswer(question, ::getAnswerToMostImportantQuestion)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fun getAnswerToMostImportantQuestion(): Int {&lt;br /&gt;
    return 42&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fun askAndAnswer(question: String?, getAnswer: () -&amp;gt; Int) {&lt;br /&gt;
    println(&amp;quot;Question: $question&amp;quot;)&lt;br /&gt;
    println(&amp;quot;Answer: ${getAnswer()}&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JavaScript ===&lt;br /&gt;
&lt;br /&gt;
In the following [[JavaScript]] code, function &amp;lt;code&amp;gt;calculate&amp;lt;/code&amp;gt; uses parameter &amp;lt;code&amp;gt;operate&amp;lt;/code&amp;gt; as a blocking callback. &amp;lt;code&amp;gt;calculate&amp;lt;/code&amp;gt; is called with &amp;lt;code&amp;gt;multiply&amp;lt;/code&amp;gt; and then with &amp;lt;code&amp;gt;sum&amp;lt;/code&amp;gt; which act as callback functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
function calculate(a, b, operate) {&lt;br /&gt;
    return operate(a, b);&lt;br /&gt;
}&lt;br /&gt;
function multiply(a, b) {&lt;br /&gt;
    return a * b;&lt;br /&gt;
}&lt;br /&gt;
function sum(a, b) {&lt;br /&gt;
    return a + b;&lt;br /&gt;
}&lt;br /&gt;
// outputs 20&lt;br /&gt;
alert(calculate(10, 2, multiply));&lt;br /&gt;
// outputs 12&lt;br /&gt;
alert(calculate(10, 2, sum));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The collection method {{code|.each()}} of the [[jQuery]] [[JavaScript libraries|library]] uses the function passed to it as a blocking callback. It calls the callback for each item of the collection. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
$(&amp;quot;li&amp;quot;).each(function(index) {&lt;br /&gt;
  console.log(index + &amp;quot;: &amp;quot; + $(this).text());&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deferred callbacks are commonly used for handling events from the user, the client and timers. Examples can be found in {{code|addEventListener}}, [[Ajax (programming)|Ajax]] and &amp;lt;code&amp;gt;[[XMLHttpRequest]]&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;ref&amp;gt;{{cite web |url=https://udn.realityripple.com/docs/Mozilla/Creating_JavaScript_callbacks_in_components#JavaScript_functions_as_callbacks |title=Creating JavaScript callbacks in components |department=Archive |website=UDN Web Docs |at=sec. JavaScript functions as callbacks |language=en |type=Documentation page |accessdate=2021-12-16 |url-status=live|archive-url=https://web.archive.org/web/20211216020616/https://udn.realityripple.com/docs/Mozilla/Creating_JavaScript_callbacks_in_components |archive-date=2021-12-16 }}&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In addition to using callbacks in JavaScript source code, C functions that take a function are supported via js-ctypes.&amp;lt;ref&amp;gt;{{cite web |url=https://developer.mozilla.org.cach3.com/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks&amp;lt;!--This page is not properly rendered--&amp;gt; |title=Declaring and Using Callbacks |editor1-last=Holley |editor1-first=Bobby |editor2-last=Shepherd |editor2-first=Eric |department=Docs |website=[[Mozilla Developer Network]] |language=en |type=Documentation page |accessdate=2021-12-16 |url-status=live |archive-url=https://web.archive.org/web/20190117092921/https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks |archive-date=2019-01-17}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Red and REBOL ===&lt;br /&gt;
&lt;br /&gt;
The following [[REBOL]]/[[Red (programming language)|Red]] code demonstrates callback use.&lt;br /&gt;
&lt;br /&gt;
* As alert requires a string, form produces a string from the result of calculate&lt;br /&gt;
* 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.&lt;br /&gt;
* The datatype! references in a block! [float! integer!] restrict the type of values passed as arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;red&amp;quot;&amp;gt;&lt;br /&gt;
Red [Title: &amp;quot;Callback example&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
calculate: func [&lt;br /&gt;
    num1 [number!] &lt;br /&gt;
    num2 [number!] &lt;br /&gt;
    callback-function [function!]&lt;br /&gt;
][&lt;br /&gt;
    callback-function num1 num2&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
calc-product: func [&lt;br /&gt;
    num1 [number!] &lt;br /&gt;
    num2 [number!]&lt;br /&gt;
][&lt;br /&gt;
    num1 * num2&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
calc-sum: func [&lt;br /&gt;
    num1 [number!] &lt;br /&gt;
    num2 [number!]&lt;br /&gt;
][&lt;br /&gt;
    num1 + num2&lt;br /&gt;
]&lt;br /&gt;
&lt;br /&gt;
; alerts 75, the product of 5 and 15&lt;br /&gt;
alert form calculate 5 15 :calc-product&lt;br /&gt;
&lt;br /&gt;
; alerts 20, the sum of 5 and 15&lt;br /&gt;
alert form calculate 5 15 :calc-sum&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Rust ===&lt;br /&gt;
[[Rust (programming language)|Rust]] have the {{Code|Fn}}, {{Code|FnMut}} and {{Code|FnOnce}} traits.&amp;lt;ref&amp;gt;{{cite web |title=Fn in std::ops - Rust |url=https://doc.rust-lang.org/std/ops/trait.Fn.html |website=doc.rust-lang.org |access-date=18 January 2025}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn call_with_one&amp;lt;F&amp;gt;(func: F) -&amp;gt; usize&lt;br /&gt;
    where F: Fn(usize) -&amp;gt; usize {&lt;br /&gt;
    func(1)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
let double = |x| x * 2;&lt;br /&gt;
assert_eq!(call_with_one(double), 2);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
In this [[Lua]] code, function {{code|calculate}} accepts the {{code|operation}} parameter which is used as a blocking callback. {{code|calculate}} is called with both {{code|add}} and {{code|multiply}}, and then uses an [[anonymous function]] to divide. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;function calculate(a, b, operation)&lt;br /&gt;
    return operation(a, b)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function multiply(a, b)&lt;br /&gt;
    return a * b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function add(a, b)&lt;br /&gt;
    return a + b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
print(calculate(10, 20, multiply)) -- outputs 200&lt;br /&gt;
print(calculate(10, 20, add)) -- outputs 30&lt;br /&gt;
-- an example of a callback using an anonymous function&lt;br /&gt;
print(calculate(10, 20, function(a, b)&lt;br /&gt;
    return a / b -- outputs 0.5&lt;br /&gt;
end))&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&lt;br /&gt;
In the following [[Python (programming language)|Python]] code, function {{code|calculate}} accepts a parameter {{code|operate}} that is used as a blocking callback. {{code|calculate}} is called with {{code|square}} which acts as a callback function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def square(val: int) -&amp;gt; int:&lt;br /&gt;
    return val ** 2&lt;br /&gt;
&lt;br /&gt;
def calculate(operate: Callable[[int], int], val: int) -&amp;gt; int:&lt;br /&gt;
    return operate(val)&lt;br /&gt;
&lt;br /&gt;
# prints: 25&lt;br /&gt;
print(calculate(square, 5))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Julia===&lt;br /&gt;
&lt;br /&gt;
In the following [[Julia (programming language)|Julia]] code, function {{code|calculate}} accepts a parameter {{code|operate}} that is used as a blocking callback. {{code|calculate}} is called with {{code|square}} which acts as a callback function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;jlcon&amp;quot;&amp;gt;&lt;br /&gt;
julia&amp;gt; square(val) = val^2&lt;br /&gt;
square (generic function with 1 method)&lt;br /&gt;
julia&amp;gt; calculate(operate, val) = operate(val)&lt;br /&gt;
calculate (generic function with 1 method)&lt;br /&gt;
julia&amp;gt; calculate(square, 5)&lt;br /&gt;
25&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
{{Portal|Computer programming}}&lt;br /&gt;
&lt;br /&gt;
{{Div col|colwidth=25em}}&lt;br /&gt;
* [[Command pattern]]&lt;br /&gt;
* [[Continuation-passing style]]&lt;br /&gt;
* [[Event loop]]&lt;br /&gt;
* [[Event-driven programming]]&lt;br /&gt;
* [[Implicit invocation]]&lt;br /&gt;
* [[Inversion of control]]&lt;br /&gt;
* [[libsigc++]], a callback library for C++&lt;br /&gt;
* [[Signals and slots]]&lt;br /&gt;
* [[User exit]]&lt;br /&gt;
{{div col end}}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/december/using-net-implementing-callback-notifications-using-delegates Basic Instincts: Implementing Callback Notifications Using Delegates] - [[MSDN Magazine]], December 2002&lt;br /&gt;
* [https://web.archive.org/web/20080916192721/http://www.javaworld.com/javaworld/javatips/jw-javatip10.html Implement callback routines in Java]&lt;br /&gt;
* [https://www.codeproject.com/Articles/7865/Implement-Script-Callback-Framework-in-ASP-NET-1-x Implement Script Callback Framework in ASP.NET 1.x] - Code Project, 2 August 2004&lt;br /&gt;
* Interfacing C++ member functions with C libraries (archived from the original on July 6, 2011)&lt;br /&gt;
* [http://gotw.ca/gotw/083.htm Style Case Study #2: Generic Callbacks]&lt;br /&gt;
&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:Articles with example C Sharp code]]&lt;br /&gt;
[[Category:Articles with example JavaScript code]]&lt;br /&gt;
[[Category:Articles with example Julia code]]&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;br /&gt;
[[Category:Articles with example Rust code]]&lt;br /&gt;
[[Category:Subroutines]]&lt;/div&gt;</summary>
		<author><name>imported&gt;LizardJr8</name></author>
	</entry>
</feed>