What is a callback function?
相关问题
- Disable Browser onUnload on certain links?
- Jscrollpane - Fires a function when isAtBottom
- How to pass C# method as a callback to CLI/C++ fun
- Name for a method that has only side effects
- Callbacks with Python curses
相关文章
- How to create a CFuncType in Python
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Change loss function dynamically during training i
- Is there an existing solution for these particular
- What is Scope Creep? [closed]
- What is the best way to call into Swift from C?
- How can I modify .xfdl files? (Update #1)
Assume we have a function
sort(int *arraytobesorted,void (*algorithmchosen)(void))
where it can accept a function pointer as its argument which can be used at some point insort()
's implementation . Then , here the code that is being addressed by the function pointeralgorithmchosen
is called as callback function .And see the advantage is that we can choose any algorithm like:
Which were, say,have been implemented with the prototype:
This is a concept used in achieving Polymorphism in Object Oriented Programming
Let's keep it simple. What is a call back function?
Example by Parable and Analogy
I have a secretary. Everyday I ask her to: (i) drop off the firm's outgoing mail at the post office, and after she's done that, to do: (ii) whatever task I wrote for her on one of those sticky notes.
Now, what is the task on the sticky-note? The task varies from day to day.
Suppose on this particular day, I require her to print off some documents. So I write that down on the sticky note, and I pin it on her desk along with the outgoing mail she needs to post.
In summary:
The call back function is that second task: printing off those documents. Because it is done AFTER the mail is dropped off, and also because the sticky note telling her to print the document is given to her along with the mail she needs to most.
Let's now tie this in with programming vocabulary
That's all it is. Nothing more. I hope that cleared it up for you - and if not, post a comment and I'll do my best to clarify.
a callback function is a function that is passed to another function (let's call this other function
otherFunction
) as a parameter, and the callback function is called (or executed) inside theotherFunction
.function action(x, y, callback) { return callback(x, y); }
function multiplication(x, y) { return x * y; }
function addition(x, y) { return x + y; }
alert(action(10, 10, multiplication)); // output: 100
alert(action(10, 10, addition)); // output: 20
In SOA, callback allows the Plugin Modules to access services from the container/environment.
Analogy: Callbacks. Asynchronous. Non-blocking
Real life example for callback
The simple answer to this question is that a callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made
Opaque Definition
A callback function is a function you provide to another piece of code, allowing it to be called by that code.
Contrived example
Why would you want to do this? Let's say there is a service you need to invoke. If the service returns immediately, you just:
For example, suppose the service were the
factorial
function. When you want the value of5!
, you would invokefactorial(5)
, and the following steps would occur:Your current execution location is saved (on the stack, but that's not important)
Execution is handed over to
factorial
When
factorial
completes, it puts the result somewhere you can get to itExecution comes back to where it was in [1]
Now suppose
factorial
took a really long time, because you're giving it huge numbers and it needs to run on some supercomputing cluster somwhere. Let's say you expect it to take 5 minutes to return your result. You could:Keep your design and run your program at night when you're asleep, so that you're not staring at the screen half the time
Design your program to do other things while
factorial
is doing its thingIf you choose the second option, then callbacks might work for you.
End-to-end design
In order to exploit a callback pattern, what you want is to be able to call
factorial
in the following way:The second parameter,
what_to_do_with_the_result
, is a function you send along tofactorial
, in the hope thatfactorial
will call it on its result before returning.Yes, this means that
factorial
needs to have been written to support callbacks.Now suppose that you want to be able to pass a parameter to your callback. Now you can't, because you're not going to be calling it,
factorial
is. Sofactorial
needs to be written to allow you to pass your parameters in, and it will just hand them over to your callback when it invokes it. It might look like this:Now that
factorial
allows this pattern, your callback might look like this:and your call to
factorial
would beWhat if you want to return something from
logIt
? Well, you can't, becausefactorial
isn't paying attention to it.Well, why can't
factorial
just return what your callback returns?Making it non-blocking
Since execution is meant to be handed over to the callback when
factorial
is finished, it really shouldn't return anything to its caller. And ideally, it would somehow launch its work in another thread / process / machine and return immediately so that you can continue, maybe something like this:This is now an "asynchronous call", meaning that when you call it, it returns immediately but hasn't really done its job yet. So you do need mechanisms to check on it, and to obtain its result when its finished, and your program has gotten more complex in the process.
And by the way, using this pattern the
factorial_worker_task
can launch your callback asynchronously and return immediately.So what do you do?
The answer is to stay within the callback pattern. Whenever you want to write
and
f
is to be called asynchronously, you will instead writewhere
g
is passed as a callback.This fundamentally changes the flow-topology of your program, and takes some getting used to.
Your programming language could help you a lot by giving you a way to create functions on-the-fly. In the code immediately above, the function
g
might be as small asprint (2*a+1)
. If your language requires that you define this as a separate function, with an entirely unnecessary name and signature, then your life is going to get unpleasant if you use this pattern a lot.If, on the other hand, you language allows you to create lambdas, then you are in much better shape. You will then end up writing something like
which is so much nicer.
How to pass the callback
How would you pass the callback function to
factorial
? Well, you could do it in a number of ways.If the called function is running in the same process, you could pass a function pointer
Or maybe you want to maintain a dictionary of
fn name --> fn ptr
in your program, in which case you could pass the nameMaybe your language allows you to define the function in-place, possible as a lambda! Internally it is creating some kind of object and passing a pointer, but you don't have to worry about that.
Perhaps the function you are calling is running on an entirely separate machine, and you are calling it using a network protocol like HTTP. You could expose your callback as an HTTP-callable function, and pass its URL.
You get the idea.
The recent rise of callbacks
In this web era we have entered, the services we invoke are often over the network. We often do not have any control over those services i.e. we didn't write them, we don't maintain them, we can't ensure they're up or how they're performing.
But we can't expect our programs to block while we're waiting for these services to respond. Being aware of this, the service providers often design APIs using the callback pattern.
JavaScript supports callbacks very nicely e.g. with lambdas and closures. And there is a lot of activity in the JavaScript world, both on the browser as well as on the server. There are even JavaScript platforms being developed for mobile.
As we move forward, more and more of us will be writing asynchronous code, for which this understanding will be essential.
Note that callback is one word.
The wikipedia callback page explains it very well.
quote from wikipedia page: