Difference between revisions of "local"
(Create page) |
m (Add code comment) |
||
Line 5: | Line 5: | ||
== Usage == | == Usage == | ||
<source lang="lua"> | <source lang="lua"> | ||
− | local foo = "bar" | + | local foo = "bar" -- without the word local before it, foo would be global |
</source> | </source> | ||
== See Also == | == See Also == | ||
[http://www.lua.org/pil/4.2.html Programming in Lua] | [http://www.lua.org/pil/4.2.html Programming in Lua] |
Revision as of 20:19, 31 July 2011
In Lua, variables can be global or local. Unlike global variables, local variables have their scope limited to the block where they are declared.
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.
Usage
local foo = "bar" -- without the word local before it, foo would be global