views:

180

answers:

3

Is it a function?
Is it a function being called from the source?
Or, is it a function being returned from the destination?
Or, is it just executing a function at the destination?
Or, is it a value returned from a function passed to the destination?

+5  A: 

From the great Wikipedia:

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

Said another way, when you pass a callback to your method, it's as if you are providing additional instructions (e.g., what you should do next). An attempt at making a simple human example follows:

  1. Paint this wall this shade of green (where "paint" is analagous to the method called, while "wall" and "green" are similar to arguments).
  2. When you have finished painting, call me at this number to let me know that you're done and I'll tell you what to do next.

In terms of practical applications, one place where you will sometimes see callbacks is in situations with asynchronous message passing. You might want to register a particular message as an item of interest for class B.

However, without something like a callback, there's no obvious way for class A to know that class B has received the message. With a callback, you can tell class B, here's the message that I want you to listen for and this is the method in class A that I want you to call when you receive it.

Here is a Java example of a callback from a related question.

Bob Cross
+1 15 seconds faster!
Andrew Hare
+7  A: 

A callback is the building block of asynchronous processing.

Think of it this way: when you call someone and they don't answer, you leave a message and your phone number. Later on, the person calls you back based on the phone number you left.

A callback works in a similar manner. You ask an API for a long running operation and you provide a method from within your code to be called with the result of the operation. The API does its work and when the result is ready, it calls your callback method.

diciu
Nice illustration.
DOK
Callbacks are not only for asynchronous processing - they can be used synchronously.
Jonathan Leffler
A: 

Thank you guys.....

qstar