Solved

Callback Functions

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

19 Experts available now in Live!

Get 1:1 Help Now