3.1.1 Scope

The ‘local‘ keyword makes these “local variables.” So what does that mean?

Local variables are variables that can only be accessed from some area of the program that encompasses that variable with the purpose of limiting access.

To understand what that means, first we have to understand scope.

Scope is the region of code in a computer program where some associated set of data is active and accessible. If the program moves “outside” of that scope of that data, it is no longer accessible.

In an if statement, when we enter one of its branch, the data and code in that branch becomes valid and the CPU can access it. The scope is anything within that branch.

Because other branches are outside of this scope, we likely would not want other branches to be able to access that code.

Lua
if true then
	local var = 15
	print(var)
end
print(var)

--Output: 15
--Output: nil

’15’ is printed because that is the valid scope, making the variable valid. But after that branch exits, the variable var no longer exists and attempting to print it outputs nil.


When a nested scope is entered, the outer “parent” scope remains valid.

Lua
if true then
	local var = "Hello world!"
	
	if true then
		print(var)
	end
end
--Output: Hello world!

Even though the local variable var was created outside of the nested if statement, var can still be accessed from the nested if because the scope of the outer statement is still valid.


If a local variable is created inside a nested scope and shares a name with a variable from the encompassing scope, this variable is local only to that scope and does not override the variable from its encompassing scope.

Lua
```lua
if true then
	local var = "Hello world!"
	
	if true then
		local var = 41
		var = var + 1
		print(var)
	end
	
	print(var)
end
--Ouptut: 42
--Output: Hello world!

do-end Blocks

Occasionally you might want to deliberately limit the scope of some block of code.

For instance, a script with some initialization routine to calculate the sum for a variable based on other variables. After which we no longer need the other set of variables:

Lua
local sum = 0

local throwawayVariable1 = 42
local throwawayVariable2 = 3.14

sum = throwawayVariable1 + throwawayVariable2

print(sum)
print(throwawayVariable1)
print(throwawayVariable2)

--Output: 45.14
--Output: 42
--Output: 3.14

throwawayVariable1 and throwawayVariable2 still hang around after we’ve determined the sum.

A ‘do-end‘ block can be used as an “artificial” scope to contain the block of code.

Lua
local sum = 0

do
	local throwawayVariable1 = 42
	local throwawayVariable2 = 3.14

	sum = throwawayVariable1 + throwawayVariable2
end

print(sum)
print(throwawayVariable1)
print(throwawayVariable2)

--Output: 45.14
--Output: nil
--Output: nil

Now, after the block exits, the throwaway variables are cleaned up.

Sometimes you might also see the variable declaration on the same line as the ‘do’ keyword. Both of these styles are functionally identical.

Now, after the block exits, the throwaway variables are cleaned up.


Sometimes you might also see the variable declaration on the same line as the ‘do’ keyword. Both of these styles are functionally identical.

Lua
local sum do
---
---Some code
---
end