What is a callback function?
-
Can you help me understand intuitively the concepts behind callback functions in JS or android/windows phone 8 programming?
-
Answer:
As an analogy, it's sort of like giving your phone number to someone, and telling her to call you when she sees a specific event. That's the call-back. To follow 's explanation, you're saying "hey, Ms Save Button, when you see a click event, could you please call this function?"
Tara Dockery at Quora Visit the source
Other answers
A callback function gets called later, at an unknown time. It's old terminology which harkens back to the first GUI's. So your program fires and all sorts of things are created and initialized. On the screen is a Save button. You would assign a callback function to the Save button, to be executed when it's pressed. Callbacks may be invoked by user-events like a click, timers, or lots of other mechanisms. Another example is you may do something which takes some time, like an Ajax call to a web server. You don't want to wait around being unresponsive while the call completes, so you fire it asynchronously with a callback. Meaning, your JS continues immediately after submitting the request. Later, when your Ajax call has the data you want, your callback is invoked. Callbacks are a way to handle events which occur at unknown times.
Christopher Reiss
There is a simple example in JS: function mySandwich(param1, param2, callback) { alert("There we start to eat sandwich. Parameters: " + param1 + " , " + param2); callback(); } mySandwich("ham", "cheese", function() { alert("Finish eating our sandwich"); }); There we have function mySandwich which takes three parameters. The third one is callback function. While main function is executing it generates message with output parameters. And then callback function executes. The code of the callback function is defined in the third argument of the main function. The callback function just displays a message that it's been executed. Sorry for my English if I made mistakes.
Sergey Kasatikov
The logic of callback functions remains the same irrespective of the language. I will explain it using C. A callback function is one which is passed as an argument to another function and is invoked after the completion of the parent function. In other words callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback. #include <stdio.h> int a(int (*callback)(void)) { printf("inside parent function"); callback(); } int test() { printf("inside callback function"); } int main (void) { a(&test); printf("main gotta end"); return 0; } In this example what we want to do is to call our callback function test when the function a is finished with its execution. What we do is that we pass the address of our callback function test to the function a . After we are done with the code of function a we call the callback function by calling callback(), internally this function will call our required callback function test as we have passed it as an argument. Hope it helps to clear your understanding regarding callback functions. This concept is very useful for application as well as system developers.
Prabhash Kumar Jha
The answer is explained with the help of JavaScript code. Whenever we set up a function to be called at a later time, whether by the browser or other code, weâre setting up what is termed a callback. The term stems from the fact that we establish a function that some other code will later âcall backâ into at an appropriate point of execution. function useless(callback) { // few lines of code... return callback(); } function myCallback(){ console.log('Thats the way love goes ..'); } useless(myCallback); In this case myCallback is a function which is passed as a parameter to the useless function. At the end of useless function we have invoked the callback passed as parameter. This is taken from the book : by .
Manish Dipankar
An 8-year old explanation could go like this : A callback function is a function that says âI am just a simple function. Please call me when a complex function completes its work. Just put my name in the complex functionâs argumentâ.Lets say your complex function is named computeSummary(). This functionâs job is to create a summary report of your companyâs 1 billion users. In the real world, the processing of this complex function can take several minutes/hours. But immediately after your complex functionâs computation is done, lets say you need to send(email) the results to your companyâs CEO. For this you can write a simple sendSummary() function. This simple sendSummary function is an example of a callback function.Its like saying : Execute the complex function. Immediately after that, execute the simple function.In asynchronous programming, the setup (âworkflowâ) could look like the below : Run your complex computeSummary function (create event1) Run another complex computeSummary function (create event2) Event 2 is done. So run the simple sendSummary function (create event3) Event 1 is done. So run the simple sendSummary function (create event4) As you can see above, event2 could get completed before event1 (asynchronous events). So the simple âcallbackâ function sendSummary gets executed (event3) immediately after complex event2.Typical (newbie) example of callback functions are : print or display something on screen commit or save something to a database send email to users log errors to a file
Thyag Sundaramoorthy
The mechanism of calling a function from another function by passing its memory address is known as callback function. Callbacks are achieved in java with the help of inerface references.
Vasavi Boda
I have tried to explain it here. https://knownodejs.quora.com/Callback-functions
Lovy Gupta
Related Q & A:
- how to estimate the phase parameter of a complex function?Best solution by Mathematics
- How to speed up a wordpress function with multiple loops?Best solution by WordPress
- How to find the derivative without using a symbolic function in Matlab?Best solution by Stack Overflow
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- How to graph a quadratic function on a graphing calculator?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.