How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer?
相关问题
- Keeping track of variable instances
- Disable Browser onUnload on certain links?
- How to get the maximum of more than 2 numbers in V
- F#: Storing and mapping a list of functions
- Jscrollpane - Fires a function when isAtBottom
相关文章
- How to create a CFuncType in Python
- Accessing an array element when returning from a f
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Change loss function dynamically during training i
- Equivalent to window.setTimeout() for C++
- How can I write-protect the Matlab language?
- Java Equivalent to iif function
Usually we sent variables to functions . Suppose you have task where the variable needs to be processed before being given as an argument - you can use callback .
function1(var1, var2)
is the usual way .What if I want
var2
to be processed and then sent as an argument?function1(var1, function2(var2))
This is one type of callback - where
function2
executes some code and returns a variable back to the initial function .Often an application needs to execute different functions based upon its context/state. For this, we use a variable where we would store the information about the function to be called. According to its need the application will set this variable with the information about function to be called and will call the function using the same variable.
In javascript, the example is below. Here we use method argument as a variable where we store information about function.
Imagine you need a function that returns 10 squared so you write a function:
Later you need 9 squared so you write another function:
Eventually you will replace all of these with a generic function:
The exact same thinking applies for callbacks. You have a function that does something and when done calls doA:
Later you want the exact same function to call doB instead you could duplicate the whole function:
Or you could pass a callback function as a variable and only have to have the function once:
Then you just have to call compute(doA) and compute(doB).
Beyond simplifying code, it lets asynchronous code let you know it has completed by calling your arbitrary function on completion, similar to when you call someone on the phone and leave a callback number.
For teaching callbacks, you have to teach the pointer first. Once the students understand the idea of pointer to a variable, idea of callbacks will get easier. Assuming you are using C/C++, these steps can be followed.
There might many more things. Involve the students and they will discover. Hope this helps.
I think it's an rather easy task to explain.
At first callback are just ordinary functions.
And the further is, that we call this function (let's call it A) from inside another function (let's call it B).
The magic about this is that I decide, which function should be called by the function from outside B.
At the time I write the function B I don't know which callback function should be called. At the time I call function B I also tell this function to call function A. That is all.
Callbacks allows you to insert your own code into another block of code to be executed at another time, that modifies or adds to the behavior of that other block of code to suit your needs. You gain flexibility and customizability while being able to have more maintainable code.
Less hardcode = easier to maintain and change = less time = more business value = awesomeness.
For example, in javascript, using Underscore.js, you could find all even elements in an array like this:
Example courtesy of Underscore.js: http://documentcloud.github.com/underscore/#filter