What is a callback function?
Note that callback is one word.
The wikipedia callback page explains it very well.
quote from wikipedia page:
Think of it as an "In case of fire, break glass" subroutine. Many computer programs tend to be written such that they expect a certain set of possibilities at any given moment. If "Thing That Was Expected", then "Do something", otherwise, "Do something else." is a common theme. However, there are many situations in which events (such as fire) could happen at any time. Rather than checking for them at each possible step ("Thing that was expected OR Things are on fire"), it is easier to have a system which detects a number of events, and will call the appropriate function upon said event (this also keeps us from having to write programs like "Thing that was expected OR Things are on fire OR Nuclear meltdown OR alien invasion OR the dead rising from the grave OR...etc., etc.) Instead, a callback routine is a sort of insurance policy. If zombies attack, call this function. If the user moves their mouse over an icon, call HighlightIcon, and so forth.
From what I think, a callback function is a function you specify to an existing function/method, to be invoked when an action is completed.
In Javascript, or more specifically jQuery, for example, you can specify a callback argument to be called when an animation has finished.
In PHP, the preg_replace_callback()
function allows you to provide a function that will be called when the regular expression is matched, passing the string(s) matched as arguments.
This concept wasn't taught me in school, but when I started working I saw its being used at quite frequently at many places.
One important usage area is that you register one of your function as a handle (i.e. a callback) and then send a message / call some function to do some work or processing. Now after the processing is done, the called function would call our registered function (i.e. now call back is done), thus indicating us processing is done.
This wikipedia link explains quite well graphically.
A callback function is one that should be called when a certain condition is met. Instead of being called immediately, the callback function is called at a certain point in the future.
Typically it is used when a task is being started that will finish asynchronously (ie will finish some time after the calling function has returned).
For example, a function to request a webpage might require its caller to provide a callback function that will be called when the webpage has finished downloading.
A callback is a function which you pass to an API to be called at a later time.
The most obvious case is an event handler in a GUI. Take Javascript as a very simple example:
<script>
function interact()
{
var name = prompt("What is your name?");
alert("Hi, " + name);
}
</script>
<button onclick="interact()">Interact</button>
Here, onclick="interact()" is telling Javascript to call the interact function when the button is clicked. In the code, we never actually call interact() ourselves. It's always done by the browser for us. We pass it the function and it calls it at a later time for us.
Another example is using Ajax in Javascript. In Ajax, there are three parts.
- Send a query to the web server
- Wait for the web server to respond
- Process the web server's response
After step 1, our Javascript program idles. It sits patiently in the background, listening for an incoming call from the web server. When it gets one, what does the browser do? Answer: we have to pass it a callback in step 1 and it executes it in step 3.
This makes callbacks sound like return statements at the end of methods.
I'm not sure that's what they are.
I think Callbacks are actually a call to a function, as a consequence of another function being invoked and completing.
I also think Callbacks are meant to address the originating invocation, in a kind of "hey! that thing you asked for? I've done it - just thought I would let you know - back over to you".