Solved

Callback Functions

Posted on 2014-02-02
5
283 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 70

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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