0
$\begingroup$

Although I seriously code with computer languages in general since 2010 and as an amateur programmer with programming languages in particular since 2015 (primarily Bash and JavaScript imperative scripts) and codes I wrote are scattered through my Stack Exchange accounts, as in my Code Review SE account,
I think I still misunderstand what a Callback is;
I feel that the term itself might be misleading, as I understand that callback functions doesn't "call back" anything, but rather "called back" as a reaction to a condition (event).
I understand the alternative term Callafter as significantly controversial between programmers, so I stay with "Callback" or its semantic siblings;
Any Stack Exchange session I read about "Callback" contained answers that I either recognized as contradicting or controversial by other programmers commenting in comments.

My question

Because the particular examples I read of this concept in Bash and JavaScript are not remembered to me as clear, I ask:

Does the callback concept of programming have any basis in computer science?

That is to ask; is there, as part of a computer science theory, some logical formula that can simulate all possible "callback functions" (similarly to as a Turing machine can simulate all possible functions of a computer) that might help understanding the purpose of this concept?
An answer might help me understand what is the common denominator (if there is one) between different "callback" terms in programming.

$\endgroup$
  • 1
    $\begingroup$ I might be misunderstanding your question, but note that "callback function" is contextual. A sort routine may use "less than" or "greater than" callbacks to sort a list in ascending or descending order, but you can still use "less than" in ordinary non-callback contexts. $\endgroup$ – MSalters Jan 20 at 11:15
  • $\begingroup$ Related: retrocomputing.stackexchange.com/questions/13479/… $\endgroup$ – user109446 Feb 2 at 9:12
3
+50
$\begingroup$

(I might be wrong, but this is my understanding.)

Callbacks, especially as seen in JS, are analogous to a semantic model known as Continuation-passing style (CPS). In CPS, a program’s flow is modeled as a series of functions, each of which takes in a continuation, and calls the continuation with the function’s result when finished.

As stated, this is also a form of higher-order programming, where functions are used as values.

| cite | improve this answer | |
$\endgroup$
2
$\begingroup$

Yes, callbacks belong to to the family of Higer-order functions. Because they're one of the simplest members of the family, they're often encountered in practice.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Hello ; I ran a search query in the linked Wikipedia article for callback and found only a link to the Callback Wikipedia article itself, which I find unclear and found unclear in the past and consider as a non didactic learning material at least now for me personally; hence, please explain what you mean by family member of higher order functions; otherwise, sadly and with all honesty, the answer doesn't help me to understand the concept. $\endgroup$ – user109446 Jan 20 at 12:41
  • $\begingroup$ @JohnDoea: Searching for just the word "callback" indeed gives you little results. For instance, it would miss the link to the map page, which is a basic example of a function taking a callback (applies a callback function to all elements of a list). All the examples on the higher-order function page are in fact examples of callbacks - add3 is passed as the callback function. $\endgroup$ – MSalters Jan 20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy