Anonymous function

An anonymous function or lambda function is a function in a computer program that can not be addressed by their name, but only through references as references or pointers.

Named functions

The concept of named functions is known in virtually every popular programming language: A function receives when they are declared a unique name under which it is subsequently addressed.

# Declare and define the function sub benannteFunktion    {    Code ... ...    }   # Call the function benannteFunktion (...); The function name is used by the runtime system to identify by means of the symbol table or a dynamic process, the function definition and then execute the stored code.

/ / Declaration and definition funktion_mit_name int (void) {      / / ... Code ... } In compiled languages ​​such as C or C solves similar in principle already the linker the names of non- virtual functions.

Anonymous Functions

With the concept of anonymous functions, it is possible to define not only named and unnamed functions.

(lambda (x ) ( x * 2) ) In order to use this function that doubles a value in the same train, it can be applied in function position directly to the exemplary argument 5:

( (lambda (x ) ( x * 2) ) 5) What the Lisp system with

10 hear the answering. This is followed by the function definition, since it is not bound by a symbol, for garbage.

# Declaration and definition of the function and assignment to a (reference) variable my $ function- reference = sub { ...};   # Call the function $ function- reference -> (...); Here, since the function has a name, a symbolic call is no longer possible. Rather it is a reference, addressed a reference to the definition, indirectly.

Auto funktions_referenz = delegate int ( string str) {int x; / * ... * / Return x; };   funktions_referenz ( "Hello World! "); Anonymous functions can greatly simplify the programming of context-dependent function calls. In addition, they allow the easy use of function references as function arguments, for example, to transfer a callback function (callback ), and storage in data structures.

[ capture ] ( params ) -> ret {body }; capture: transfer of the specified variables in the scope of the lambda expression params: transfer parameters -> Ret: return type (optional; arrow required) body: function body

/ / Because almost every lambda function has a different type, a function template is used: template void rueckrufendeFunktion (lambda type anonyme_funktion ) {      anonyme_funktion (); }   int main () {      car fun = [ ] { ...};        rueckrufendeFunktion (fun);        return 0; } The related concept of function pointers in the C programming language offers similar options, but is not trivial due to its syntax in the application. In addition, there the actual function separately defined references remain the same name, but it can have any name.

In C function pointers can be replaced by lambda Audrücke that as shown in the examples, as in Perl or other languages ​​can be used.

In Java 8 closures can be used for this purpose anonymous inner classes and Version.

Anonymous functions are supported by most scripting languages ​​, such as Perl, Python, Lisp and - in more recent versions - JavaScript and JScript.

Pictures of Anonymous function

67569
de