Closures and Upvalues
Lua supports closures, which are functions that capture variables from their surrounding scope.
Example:
function outer()
local count = 0
return function()
count = count + 1
return count
end
end
counter = outer()
print(counter()) -- Output: 1
print(counter()) -- Output: 2
Explanation: The inner function remembers the value of count
even after outer()
finishes execution. This is a closure in action.