Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Callback Functions

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
Lawrence Avery
Asked:
Lawrence Avery
  • 2
  • 2
3 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Lawrence AverySystem DeveloperAuthor Commented:
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
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
Lawrence AverySystem DeveloperAuthor Commented:
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
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now