Here’s my code, I confuse the local variable ‘count’ in the return function(c1,c2) with memory strack and where does they store in?
function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
c1 = make_counter()
c2 = make_counter()
print(c1())--print->1
print(c1())--print->2
print(c1())--print->3
print(c2())--print->1
print(c2())--print->2
It’s stored in the closure!
c1is not a closure, it is the function returned bymake_counter(). The closure is not explicitly declared anywhere. It is the combination of the function returned bymake_counter()and the “free variables” of that function. See closures @ Wikipedia, specifically the implementation: