x=5 -- Global Variable do -- Start of a block 1 local x=x -- Local Variable print (x) -- Prints 5 x=x+2 -- Modifies local x print (x) -- Prints 7 do -- Start of block 2 local x=x+4 -- Yet another local x :) print (x) -- prints 11 end -- End of block 2 print (x) -- Prints 7 end -- End of block 1 print (x) -- Prints 5, global variableNotice that we assign the value of x in the previous block to the x in the next block. This is possible because the local variables can be freely accessed by the functions defined inside their scope. The local variable used by the inner function is called an upvalue or external local variable, inside the inner function.
Marcadores: Computador, English, expressions, Language, Lua, manual, mod_lua, Scope, Scripting, Variable, variables