The idea of a local variable comes from the idea of scope.
The scope of a name (or equivalently, the data associated with that name, as in a variable) means where/when you can use that name to refer to that thing.
The default scope of variables (that are not iterators in for loops or function parameters) is global.
That is, they are not local --- their scope is the entire script (hence, global)
In good code, in modern programming languages, scopes are limited -- that is, they aren't the entire script (global).
Scopes are limited so that:
- To help avoid mistakes
- To allow recursion
- To allow name-reuse
For example, consider this function:
function hypotenuse(a,b)
c2 = a^2 + b^2
c = math.sqrt(c2)
return c
end
Let's write some other code that makes use of it; in specific, we'll look at two versions of this code that are exactly the same, we just renamed some variables:
x = 3
y = 4
z = 12
print(z)
print( hypotenuse(x, y) )
print(z)
print( hypotenuse(y, z) )
a = 3
b = 4
c = 12
print(c)
print( hypotenuse(a, b) )
print(c)
print( hypotenuse(b, c) )
The output from them is different!
This isn't a reasonable result, since Lua shouldn't care what we name the variables.
Because hypotenuse writes to c, it also modified the c we were using to store triangle side lengths.
The reason for this is that the c in the last example is the same c as it appears in hypotenuse (even though they ought to have nothing to do with each other)
It's sometimes called "poisoning" the scope when this happens -- calling hypotenuse damages other variables. We don't want that -- we want the c in hypotenuse to only be the c in hypotenuse.
We denote that by declaring c as local which means its scope extends only within the current block (thing marked by end or until)
Here's what a fixed hypotenuse would look like:
function hypotenuse(a, b)
local c2 = a^2 + b^2
local c = math.sqrt(c2)
return c
end
a = 3
b = 4
c = 12
print(c)
print( hypotenuse(a, b) )
print(c)
print( hypotenuse(b, c) )
By default, the iterating variables of for loops and function parameters are already local.
Since functions are usually only declared at the top level, making them local doesn't usually accomplish anything.
Best practice: Always use local. It helps keep things neater and it also helps you mentally keep track of where variables should be used for the first time.
TL;DR:
- Scope is the part of the script that you can use a variable in
- Smaller scopes are usually better
- Always use
local on variables (the first time you declare them)
- Functions and parameters won't need
local (normally)
local lets same variable name be used in 2 places
- Variables default as global (not
local) (causing problems, see 3rd bullet)
5 comments
To add on: If you put a local variable outside of any function, loop, etc, the local variable acts as a global variable.
I would also like to add, I've seen some people call local variables 'locals'. This is incorrect. 'Local' is simply an adjective modifying the variable, but it's still a variable. It's the same as if you were to call 'cute cats' simply 'cutes'.
Private and public are NOT the same thing as local and global. Not at all. Lua has no capacity for private variables (because it lacks the concept of a class / "who" accesses).
Actually, Blue, I was not saying they're the the same thing. I said they're similar in the sense they share a similar scope.
It bears no resemblance. You cannot access variables in other scopes in Lua. Neither does visibility actually affect scope in Java (since the names are already separate since they belong to an instance/class) [Not to mention C doesn't have visibility].