FACTOID # 62: The four largest nations are Russia, China, USA, and Canada.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Callback (computer science)
A callback is often back on the level of the original caller.
A callback is often back on the level of the original caller.
In another common scenario, the callback is first registered and later called asynchronously.
In another common scenario, the callback is first registered and later called asynchronously.

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer. For other uses, see Modem (disambiguation). ... In telecommunications, a callback (also written as call-back) occurs when the originator of a call is immediately called back in a second call as a response. ... Image File history File links Callback-notitle. ... Image File history File links Callback-notitle. ... Image File history File links Callback-async-notitle. ... Image File history File links Callback-async-notitle. ... Programming redirects here. ... In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. ... Parameters are a way of allowing the same sequence of commands to operate on different data without re-specifying the instructions. ... An abstraction layer is a way of hiding the implementation details of a particular set of functionality. ... In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. ...


Usually, the higher-level code starts by calling a function within the lower-level code, passing to it a pointer or handle to another function. While the lower-level function executes, it may call the passed-in function any number of times to perform some subtask. In another scenario, the lower-level function registers the passed-in function as a handler that is to be called asynchronously by the lower-level at a later time in reaction to something. It has been suggested that Software pointer be merged into this article or section. ... A smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. ...


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. This can be a very powerful technique for code reuse. In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). ... Generic programming is a style of computer programming where algorithms are written in an extended grammar and are made adaptable by specifying variable parts that are then somehow instantiated later by the compiler with respect to the base grammar. ... Code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software. ...

Contents

Motivation

To understand the motivation for using callbacks, consider the problem of performing an arbitrary operation on each item in a list. One approach is to iterate over the list, operating on each object. This is the most common solution in practice, but it is not ideal; the code to manage the iterator (for example, a for statement) must be duplicated at each point in the code where the list is traversed. Furthermore, if the list is updated by an asynchronous process (for example, if an item is added or removed), the iterator might skip over items or become corrupt during the traversal. In computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. ... In computing, a process is an instance of a computer program that is being executed. ...


An alternative might be to create a new library function that performs the desired operation with appropriate synchronization. This approach still requires each new library function to contain the code to traverse the list. This solution is not acceptable for generic libraries intended for various applications; the library developer cannot anticipate every application need, and the application developer should not need to know the details of the library implementation. Illustration of an application which may use libvorbisfile. ...


Callbacks solve these shortcomings. One procedure is written to traverse the list, and this procedure uses application-provided code to operate on each item. There is a clear distinction between the library and the application without sacrificing flexibility.


A callback may also be regarded as a form of runtime binding, as discussed in the influential Design Patterns book (see the Chapter 1 summary). In computer science, binding refers to the creation of a simple reference to something which is larger and more complicated and used frequently. ... This article is about the book by Gamma et al. ...


Example

The following code in C demonstrates the use of callbacks for the specific case of searching an array for an item (in this case, the first integer greater than 5). First, the iteration approach: C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ...

 int i; for (i = 0; i < length; i++) { if (array[i] > 5) { break; } } if (i < length) { printf("Item %dn", i); } else { printf("Not foundn"); } 

Next, the callback approach:

 /* LIBRARY CODE */ int traverseWith(int array[], size_t length, int (*callback)(int index, int item, void *param), void *param) { int exitCode = 0; for (int i = 0; i < length; i++) { exitCode = callback(i, array[i], param); if (exitCode != 0) { break; } } return exitCode; } /* APPLICATION CODE */ int search (int index, int item, void *param) { if (item > 5) { *(int *)param = index; return 1; } else { return 0; } } /* (in another function) */ int index; int found; found = traverseWith(array, length, &search, &index); if (found) { printf("Item %dn", index); } else { printf("Not foundn"); } 

Note that traverseWith receives an extra parameter that the callback can use for its own purposes. Normally a callback uses such a parameter as a pointer to application data outside its own scope (in this case, the variable that receives the index). This feature is necessary only in a statically scoped language such as C or C++ (in C++ and OO languages other solutions are however possible, see below). Lexically scoped languages supporting closures (including functional programming languages) can provide access to application data automatically by callbacks referring to local variables. In Lisp, for example, the same program would look like this: In computer programming, scope is an enclosing context where values and expressions are associated. ... In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. ... Lisp is a family of computer programming languages with a long history and a distinctive fully-parenthesized syntax. ...

 ; LIBRARY CODE (defun traverseWith (array callback) (let ((exitCode nil) (i 0)) (while (and (not exitCode) (< i (length array))) (setf exitCode (funcall callback i (aref array i))) (setf i (+ i 1))) exitCode)) ; APPLICATION CODE (let (index found) (setf found (traverseWith array (lambda (idx item) (if (<= item 5) nil (progn (setf index idx) t)))))) 

The callback function is now defined at the point of use, and it refers to "index" by name. Synchronization concerns have been omitted from these examples, but they can easily be addressed in the traverseWith function. More importantly, they can be addressed, or ignored, by changing only that function.


Implementation

The form of a callback varies among programming languages. A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ...

  • C and C++ allow function pointers as arguments to other functions.
  • Several programming languages (though especially functional programming languages such as Scheme or ML) allow closures, a generalization of function pointers, as arguments to other functions.
  • Several programming languages, especially interpreted languages, allow one to pass the name of a function A as a parameter to a function B and have B call A by means of eval.
  • In object-oriented programming languages, a call can accept an object that implements some abstract interface, without specifying in detail how the object should do so. The programmer who implements that object may use the interface's methods exclusively for application-specific code. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns like Visitor, Observer, and Strategy.
  • C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms
  • C# .NET Framework provides a type-safe encapsulating reference, a 'delegate', to manage function pointers. These can be used for callback operations.
  • Perl supports subroutine references. [1][2]
  • Some systems have built-in programming languages to support extension and adaptation. These languages provide callbacks without the need for separate software development tools.

C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... Scheme is a multi-paradigm programming language. ... ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage as it was conceived to develop proof tactics in the LCF theorem prover (the language of... In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. ... In computer programming, an interpreted language is a programming language whose programs may be executed from source form, by an interpreter. ... In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval. ... An object-oriented programming language (also called an OO language) is one that allows or encourages, to some degree, object-oriented programming techniques such as encapsulation, inheritance, interfaces, and polymorphism. ... In software engineering, design patterns are standard solutions to common problems in software design. ... In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure. ... The observer pattern (sometimes known as publish/subscribe) is a design pattern used in computer programming to observe the state of an object in a program. ... In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. ... [[Im[[Image:Example. ... A function object, often called a functor or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. ... In computer science, a programming language is type safe when the language does not permit the programmer to treat a value as a type to which it does not belong. ... A delegate is a form of type-safe function pointer used in the . ... A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. ... Wikibooks has a book on the topic of Perl Programming Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...

Special cases

Callback functions are also frequently used as a means to handle exceptions arising within the low level function, as a way to enable side-effects in response to some condition, or as a way to gather operational statistics in the course of a larger computation. Interrupt handlers in an operating system respond to hardware conditions, signal handlers of a process are triggered by the operating system, and event handlers process the asynchronous input a program receives. In computer science, a side-effect is a property of a programming language function that it modifies some state other than its return value. ... An interrupt handler, also known as an interrupt service routine, is a subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt. ... An operating system (OS) is the software that manages the sharing of the resources of a computer and provides programmers with an interface used to access those resources. ... A signal is an asynchronous event transmitted between one process and another. ... An event handler is a part of a computer program created to tell the program how to act in response to a specific event (e. ...


A pure callback function is one which is purely functional (always returns the same value given the same inputs) and free of observable side-effects. Some uses of callbacks, such as the sorting example, require pure callback functions to operate correctly. Purely functional is a term in computing used to describe algorithms, data structures or programming languages that exclude destructive modifications (updates). ...


A special case of a callback is called a predicate callback, or just predicate for short. This is a pure callback function which accepts a single input value and returns a Boolean value. These types of callbacks are useful for filtering collections of values by some condition. For use in mathematics, see Boolean algebra (structure). ...


In event-driven programming, there is often use, in different forms, of the Observer pattern, which essentially allows the use of multicast callbacks, and where callbacks are registered early, to be invoked at callee's discretion (i.e. when a given event occurs). Some programming languages also have direct support for this construct (for instance .NET delegates or Qt's signals and slots). Event-driven programming is a computer programming paradigm. ... The observer pattern (sometimes known as publish/subscribe) is a design pattern used in computer programming to observe the state of an object in a program. ... A delegate is a form of type-safe function pointer used in the . ... Qt or QT may stand for: QuickTime a multimedia technology from Apple Computer A quart, a measure of volume within traditional systems of units, equal to 2 pints A quarter, a unit of mass in Imperial units, equal to 2 stones or a quarter of a long hundredweight; one quarter... Signals and slots is a concept introduced in Qt by the moc. ...


See also

Signals and slots is a concept introduced in Qt by the moc. ... Libsigc++ is a C++ librabry for typesafe callbacks. ... Implicit invocation is used by some authors for a style of software architecture in which a system is structured around event handling, using a form of callback. ... A user exit is a subroutine invoked by a software package for a predefined event in the execution of the package. ...

External links

References

  1. ^ Perl Cookbook - 11.4. Taking References to Functions. Retrieved on 2008-03-03.
  2. ^ Advanced Perl Programming - 4.2 Using Subroutine References. Retrieved on 2008-03-03.
2008 (MMVIII) is the current year, a leap year that started on Tuesday of the Anno Domini (or common era), in accordance to the Gregorian calendar. ... is the 62nd day of the year (63rd in leap years) in the Gregorian calendar. ... 2008 (MMVIII) is the current year, a leap year that started on Tuesday of the Anno Domini (or common era), in accordance to the Gregorian calendar. ... is the 62nd day of the year (63rd in leap years) in the Gregorian calendar. ...

  Results from FactBites:
 
Dirk Riehle: Dissertation, Chapter 4: Framework Design (7942 words)
A callback role type is a free role type of a framework that has a non-empty set of operations.
Callback role types are important, because having to define operations has the most visible impact on the implementation of a framework's class model in a particular programming language.
Callback role types are the role modeling equivalent of callback interfaces as defined in the previous subsection on traditional coupling mechanisms.
Callback (computer science) - Wikipedia, the free encyclopedia (1075 words)
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.
A callback may also be regarded as a form of runtime binding, as discussed in the influential Design Patterns book (see the Chapter 1 summary).
Callback functions are also frequently used as a means to handle exceptions arising within the low level function, as a way to enable side-effects in response to some condition, or as a way to gather operational statistics in the course of a larger computation.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.