I will not go into detail about the functioning since the doubt is more related to terminology.
Introducing
Each language can have its own definition. In general terms there is a more accepted definition of what each one is. There is some controversy but there are reliable formal sources about some terms that express the referred concepts.
Someone who comes from another language might find these terms wrong. In general it is interesting to use the terms that the community you are inserted uses to facilitate communication. But it is good to understand all the terms and how the resource works. If you meet someone with more academic bias she will express it in the most correct way, worrying about using the most correct term in each situation. But most people will worry more about just communicating the idea.
Terms
You can also find the terms Function Pointer, functor, Anonymous Function, first class Function, Function Object, high order Function, nested Function (this I find a little misleading), callback (which is also possible without being exactly a lambda), inline Function (I find this term even more ambiguous) and others to name the mechanism or concept. Not forgetting the translations of these terms that can also be used, it is common to speak in "anonymous function". Some of these terms deal with slightly different things. They may be defining some specific detail.
Delegate
We can say that the delegate is the mechanism used to implement the concept of Amble. In C# for example, the term lambda is used to indicate a simpler syntax, plus a delegate. Delegates are usually objects in object-oriented languages. Typically created from a class/structure with the necessary infrastructure to treat these delegates according to language rules. Example.
Code Block
One lambda is a function defined as a block of code to be executed in the future, so in some languages the term block code.
Lambda
Probably a more commonly used terms to express the concept. Strictly a lambda would be to closure that has no ability to capture variables but is not always interpreted this way. Note that I used this term preferably in every answer.
They differ from commonly known named functions that are "fixed".
Closure
The term closure is usually used when the lambda capture, enclosure, a state, possibly a variable, of the scope external to its body but within the scope in which the lambda defined. These variables can be assigned multiple names: non local, free variables or upvalues. If you finish executing a function where a lambda was defined and the return of this function is precisely the lambda which has closed a local variable, this variable will be highlighted and will go along with the lambda can be accessed at another time when it is invoked for execution.
A simple example (made in Javascript that is more universal):
function funcao() {
var x = 1;
return function (n) {
return x + n; //sempre vai retornar o argumento usado na chamada desta lambda + 1
}
}
var func = funcao();
func(2); //retorna 3
Some people use the term closure even when this catch does not occur. And I think that is not right. I understand that the lambda do not need to capture the variable in order to use the term closure, otherwise it gets very ephemeral. I believe the fact of lambda Having the ability to make this capture, even if you don’t do it in a specific instance, is what matters. And almost always (in almost all languages) a lambda has this ability, so it is frequent to use only a single term for any situation.
The term closure is usually used to indicate lexical closure. There is also the term syntatic closure closer to a macro found in languages such as Lisp.
An example of C#:
public static Func<int,int> Func() {
var x = 1;
Func<int, int> inc = p => {
x++;
return p + x;
};
return inc;
}
It would be compiled for something like this:
private class Closure {
public int x;
public int AnonymousFunction(int p) {
this.x++;
return p + this.x;
}
}
public static Func<int,int> Func() {
Closure c = new Closure();
c.x = 1;
Func<int, int> inc = c.AnonymousFunction;
return inc;
}
I put in the Github for future reference.
Miscellanea and conclusion
Note that whatever name you’re using, the concept of lambda presupposes that it is treated as a value in some way, so that it can be stored in a variable or passed as an argument of a function to be executed later Lazy Evaluation, for example. I don’t know Amble that cannot be attributed to another object. And I think that if this is not done, it completely loses the sense of its use. It is usually expected that a lambda can be returned as a result of a function. But not all languages allow this and I find it a great limitation. This occurs mainly in languages that do not have a Garbage Collector. Without it it is very difficult to implement closures in languages.
The ability to reset the signature depends more on the language having dynamic or static typing.
Use whatever term is or the form that is implemented, I find it a fundamental resource in any language. I learned its use very early and can greatly simplify many codes with it.
See the wikipedia article (always remembering that in English always gives more details and is usually more correct).
Very academic definition if you have patience. Understanding all this is important in certain software but understanding the general idea and basic concepts of this resource is enough to make your stock control, your real estate site, your app shopping list.
Some answers related to the subject:
In practice, no.
– Leonel Sanches da Silva