m
→Complex Scope
No edit summary |
m (→Complex Scope) |
||
| Line 139: | Line 139: | ||
function foo(n) -- In comments, assuming parameter n = 1 | function foo(n) -- In comments, assuming parameter n = 1 | ||
p(n); -- Prints 1 | p(n); -- Prints 1 | ||
local n = n + 1; -- Creates a new | local n = n + 1; -- Creates a new n in block scope, equal to n + 1 | ||
p(n); -- Prints 2 | p(n); -- Prints 2 | ||
if (true) then -- Entering a new code block | if (true) then -- Entering a new code block | ||
p(n); -- Prints 2 | p(n); -- Prints 2 | ||
local n = 15; -- Creates a more local | local n = 15; -- Creates a more local n, now | ||
p(n); -- Prints 15 | p(n); -- Prints 15 | ||
for x = 1, 10 do -- Note x is | for x = 1, 10 do -- Note x is implicitly declared as a local variable now, it is not accessible outside the for block | ||
-- Note each iteration is a separate block! | -- Note each iteration is a separate block! | ||
p(n); -- Prints 15 EVERY TIME (even on 2nd+ loop) | p(n); -- Prints 15 EVERY TIME (even on 2nd+ loop) | ||