CODE (Using a counter), taken from Pg 48 in Programming in Lua:
function newCounter ()
local i = 0
return function ()
i = i + 1
return i
end
end
RESULT:
c1 = newCounter ()
print(c1()) -->1
print(c1()) -->2
QUESTION:
How did calling the print function a second time result in “2” instead of “1”? More specifically, at the second time, how did it bypass “local i = 0”?
Thanks in advance!
[import]uid: 45656 topic_id: 14501 reply_id: 314501[/import]