Solved

Callback Functions

Posted on 2014-02-02
5
282 Views
Last Modified: 2014-02-02
I need to ask a stupid question. When a Callback function is called, does it normally return back to  the caller of the Callback function?
0
Comment
Question by:metro156
  • 2
  • 2
5 Comments
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 75 total points
ID: 39827844
Often used with asynchronous programming. When the task as completed, it can call back a method to complete/warn the caller it is done (with or without error)
0
 

Author Comment

by:metro156
ID: 39827897
Are you saying the callback function runs on its own thread therefore in order to get back to the caller of the callback function, it must call the caller.

Still confused about how to describe a callback function.???
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 225 total points
ID: 39827913
There are slight differences in some languages and situations, but typically, a callback function is called just like a regular function. The only difference is that the caller did not know about the function at compile time.

So, yes, typically just as when you call a function from one of your owns, the execution will return to the caller after the callback function has finished.

You might give a look at the entry for the concept in Wikipedia for an overall discussion and examples in different languages and situations: http://en.wikipedia.org/wiki/Callback_(computer_science)
0
 

Author Comment

by:metro156
ID: 39827970
Why can't you  just call the function from within the function without passing it as an argument? Just call it  ( unless the function is in unmanaged code and maybe that is one of the reasons to pass the function as an argument I guess).

I am just trying to think why this is used by passing it as an argument???
I read the  http://en.wikipedia.org/wiki/Callback_(computer_science)
but did not see why we pass the function as an argument.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 225 total points
ID: 39828037
Thing of an event, which is a callback function. It's not your role to detect a Click. It's the control that detects it and then need communicates with you to tell you that a click happened.

For this it needs to call a method, whose name it cannot know because your Click event procedure did not even exist when the code for the control was written.

You tell the control about your callback function / event method through either a Delegate, a Handles clause or a AddHandler instruction (depending on the language you use), so that it can call you back when a click event happens.

This is what a callback is used for, as a way for external code to provide you information that, because of its design, it cannot return to you by a simple return value.

In the case of an event, it is because the callback is made some time after you have told it to call you back.

In another example, there are a few functions in the Windows API that would need to return a String array. Most Windows API functions being written in C, many languages would not be able to understand the return value because the format of a String array in memory changes from one language to another. In order to make the API usable for as many language as possible, these functions will instead require a callback function that they will call many times, passing the String values one at a time instead of as an array. This enables more languages to be able to use them.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now