In computer programming, an anonymous function (function literal, lambda function, or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types. Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers. In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions. (Full article...)
Examples of anonymous functions Numerous languages support anonymous functions, or something similar.
APL Only some dialects support anonymous functions, either as dfns, in the tacit style or a combination of both.
C (non-standard extension) The anonymous function is not supported by standard C programming language, but supported by some C dialects, such as GCC and Clang.
GCC The GNU Compiler Collection (GCC) supports anonymous functions, mixed by nested functions and statement expressions. It has the form:
The following example works only with GCC. Because of how macros are expanded, the l_body cannot contain any commas outside of parentheses; GCC treats the comma as a delimiter between macro arguments. The argument l_ret_type can be removed if __typeof__ is available; in the example below using __typeof__ on array would return testtype *, which can be dereferenced for the actual value if needed.
Clang (C, C++, Objective-C, Objective-C++) Clang supports anonymous functions, called blocks, which have the form:
The type of the blocks above is T (^)(params). Using the aforementioned blocks extension and Grand Central Dispatch (libdispatch), the code could look simpler:
The code with blocks should be compiled with -fblocks and linked with -lBlocksRuntime
C++ C++ (since C++11) supports anonymous functions (technically function objects), called lambda expressions, which have the form [captures] [[front-annotations]] <template-parameters> [[annotations]] (params) specs requires (...) -> returns contract-specs { body }; each of these components is optional". If it is absent, the return type is deduced from return statements as if for a function with declared return type auto. As of C++20, template parameters can also be declared explicitly in the <template-parameters> area. This is an example lambda expression, with a [[nodiscard]] attribute:
C++ also supports closures, here called captures. Captures are defined between square brackets [and ] in the declaration of lambda expression. The mechanism allows these variables to be captured by value or by reference. The following table demonstrates this:
Variables captured by value are constant by default. Adding mutable after the parameter list makes them non-constant. C++14 and newer versions support init-capture, for example:
The following two examples demonstrate use of a lambda expression:
This computes the total of all elements in the list. The variable total is stored as a part of the lambda function's closure. Since it is a reference to the stack variable total, it can change its value.
This will cause total to be stored as a reference, but value will be stored as a copy. The capture of this is special. It can only be captured by value, not by reference. However in C++17, the current object can be captured by value (denoted by *this), or can be captured by reference (denoted by this). this can only be captured if the closest enclosing function is a non-static member function. The lambda will have the same access as the member that created it, in terms of protected/private members. If this is captured, either explicitly or implicitly, then the scope of the enclosed class members is also tested. Accessing members of this does not need explicit use of this-> syntax. The specific internal implementation can vary, but the expectation is that a lambda function that captures everything by reference will store the actual stack pointer of the function it is created in, rather than individual references to stack variables. However, because most lambda functions are small and local in scope, they are likely candidates for inlining, and thus need no added storage for references. If a closure object containing references to local variables is invoked after the innermost block scope of its creation, the behaviour is undefined. Lambda functions are function objects of an implementation-dependent type; this type's name is only available to the compiler, thus lambdas can be seen as compiler-generated types which implement a operator(). Because the type of a lambda is compiler-specified, it is usually stored in auto for type deduction. Lambdas may also be stored in std::function<R(Ts...)>, but this uses type erasure, while storing in a function pointer disallows captures in the lambda. If the user wishes to take a lambda function as a parameter, the parameter type must be a template type, or they must create a std::function or a similar object to capture the lambda value. The use of the auto keyword can help store the lambda function,
Here is an example of storing anonymous functions in variables, vectors, and arrays; and passing them as named parameters:
A lambda expression with an empty capture specification ([]) can be implicitly converted into a function pointer with the same type as the lambda was declared with. So this is legal:
… excerpt ends here. Continue reading the full article.