In functional programming, a closure is a encapsulated function that has access to an individual set of lexical scope variables.
Examples
The following Lua example demonstrates a closure very well.
function x()
local y = 0;
return () y = y+1; return y; end
end
local v = x(); -- v now holds the anonymous function and is a closure
v(); --> 1
v(); --> 2
In this snippet, x returns a function. The function has access to all local variables in x, but the variables are a copy different from those directly in x.
