Lambda vs Closure
A lambda is a short function with no name. A closure is a function that keeps the variables from where it was made. A lambda can be a closure, but the two words mean different things.
Lambda Function
A small function in one line, often with no name. You use it once.
A lambda is a small function written in one line, often with no name. You use it where a function is needed just once, like inside map or sort. Python writes it with the word lambda; JavaScript uses an arrow, like (x) => x * x. It can hold only a short piece of code. For anything longer, a normal named function is clearer.
Closure
A function that keeps the variables near it. It still has them later.
A closure is a function that keeps the variables from the place where it was made. The outer function may finish, but the inner one can still read and change them. A counter is the classic example: each call adds one to a number only the counter can see. Many languages use closures to keep private state without a class. JavaScript and Python both use them a lot.
In one line
- Lambda Function
- A small function in one line, often with no name. You use it once.
- Closure
- A function that keeps the variables near it. It still has them later.