Solved

Callback Functions

Posted on 2014-02-02
5
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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