In programming, an anonymous function is a function definition without an identifier (it can still be bound to a variable name, though!). They’re useful because they can be passed to higher-order functions or can construct the result of a higher-order function. There are two main types of anonymous functions:
- A lambda function
Mathematically speaking, lambda functions mainly come from the idea of lambda calculus, where all functions are anonymous.
Language-specific
In Python
In Python, we can declare lambda functions as follows:
lambda x: # expression goes here
lambda y: idx[y] # for exampleIn C++
Since C++11, we can construct lambdas with:
auto f = [](auto param1, auto param2) {
return param1 + param2;
}In functional programming, a closure is a function-like entity that can capture variables from the surrounding environment. Unlike regular functions, they’re able to access and modify variables from the enclosing scope.
Basically lambda functions.