Talk:Callback (computer programming)

This is an old revision of this page, as edited by Bleveret (talk | contribs) at 11:29, 13 March 2007 (User exit?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Latest comment: 18 years ago by Bleveret in topic User exit?

This topic needs more general discussion of callbacks and their use, with simpler examples that explain the concept of a callback. I am rewriting most of this article to include much more information.

Suggestion

I agree that this article needs some rewriting and simpler examples, and I make some suggestions. The long code on the page doesn't help, especially if C is not a language you are familiar with.

As part of a program A, suppose that a procedure or function B is called from A. Normally B would execute completely (perhaps including calls to other procedures/functions C, D ...) before returning control (and results) to A. With callback, A calls B, but passes to B information that allows B to call A2, a procedure or function which is part of the program A (i.e. is included in the code of A). The function of A2 may be to accept some partial results from B which can be passed back to A before execution of B is complete, or to carry out some action in A's environment that B is permitted to control. The implementation of callback is possible by passing procedures/functions as parameters but it is not the only mechanism.

Suggested example: data compression. Main program A wants to compress some data and output the compressed data in some way, such as to a file. Within A, a procedure A2 exists to accept some data and output it to a file. A calls B to pass data for compression and B compresses it, passing compressed data back by callback to A2, as and when it is convenient for B. Notice that this simplifies B, because it does not have to store all the compressed data and pass it back to A, and it also reduces the memory requirement within B, because compressed data that it no longer needs can be passed by callback to A2. However, the final return from B to A does not occur until B itself has determined that its task is finished. When this final return occurs, A knows that all the compressed data has already been output, and only requires to finalise the file output, perhaps by closing the file.

Implementation addition: in Ada, callback can be implemented by the use of generic procedure or function parameters as well as by 'pointers' (called access values in Ada) to procedures or functions passed as parameters.

Sangwine 20:39, 6 September 2006 (UTC)Reply

Example

Example: return

Well, why these functions return int, when counter assumes they return void?

Example: count

Why 9 is before 10, in hex?

  • Fixed: temporay loss of sanity. Dysprosia 12:50, 15 Dec 2003 (UTC)

Need to keep example code very simple

Ldo, I'm afraid that your changes (on 17 Sep 2004) have really gone farther than needed. This page is now overly complicated and reads more of a disertation on sorting algorithms rather than a simple encyclopedic article on the callback function. I think keeping any example to the minimal possible to show the concept of callback is best, and leave the lengthy programming textbook and best practices explanation elsewhere. Also some of your text is too gramatically casual, especially the paragraphs full of questions. - Dmeranda 06:54, 15 Oct 2004 (UTC)

The "language extension and adaptation" closure method

The article currently gives the following as one way to do callbacks:

Some systems have built-in programming languages to support extension and adaptation. These languages provide callbacks without the need for separate software development tools.

Can someone turn this into a more concrete claim? What are people thinking of here? LISP macros, perhaps? --Ryguasu 29 June 2005 19:00 (UTC)


This article is difficult to understand

I'm a software engineer (a real one (yeah, I know this is a very inflammatory way to start ;))) and I have difficulties understanding this article. For example, in the Motivation section it is mentioned that the code of the iterator must be duplicated everywhere the list must be iterated over. Why? Are we talking about the machine code (even then, I wouldn't understand why it would need to be inlined) or about the high-level language code? E.g. in Java, when I have to iterate over a Collection, I don't see myself duplicating any code. --132.210.56.50 15:21, 28 February 2006 (UTC)Reply

Me again. After reading the code examples that come right after, I got it.. we were talking about the high-level language repetition of the "get the iterator/loop until itr.next() returns null" stuff. Maybe it should be made explicit in the text? I'm too slow? ;) --132.210.56.50 15:30, 28 February 2006 (UTC)Reply

Now that I've gotten some distance from it, yeah, it is pretty difficult to understand. It assumes familiarity with many concepts and terms, and the example is somewhat motivated. I've made a note to revise it. Gazpacho 19:48, 30 March 2006 (UTC)Reply

I think what he means is that the entire code block within the loop must be run again each time the index is increased.

Callbacks > Polymorphism??

I don't agree with this passage:

A callback can be used as a simpler alternative to polymorphism and generic programming, in that the exact behavior of a function can be dynamically determined by passing different (yet compatible) function pointers or handles to the lower-level function.

That's at least misleading. It sounds like it's advocating using function pointers rather than polymorphism because "the exact behavior of a function can be dynamically determined by passing different ... function pointers". Polymorphism and generic programming can do the same thing (at least as well, IMHO). You might be able to argue that function pointers are simpler, buy anyone who has tried to use them in C++ knows that (1) the syntax is horrific and (2) using member function pointers for callbacks is a nightmare. By the way, it may be worth mentioning boost::function somewhere in the article, which is a superb library that uses generic programming to make function pointer-like objects which can be used very effectively for callbacks.

User exit?

A friend just did a wikipedia search on "user exit" and was brought to this page. They then checked with me as it seemed wrong. I don't agree that a Callback is the same thing as a user exit. At a generic high level a user exit is simply a break in current execution where some 3rd party code or another application is run. I believe the term comes from the need/ability for a user to exit from an application run a command in the shell for example and then return. More commonly now user exits are automatic in that they happen without human intervention where someone wants to execute a script or some code in the middle of something else. A callback is something else entirely (as described correctly in the article)... thoughts? --Bleveret 11:29, 13 March 2007 (UTC)Reply