Lua for Python Programmers

Subprograms

The scope of all variables in Lua is global unless the local keyword is used. The only exception to this is the counting variable in a counting loop which is local to the body of the loop.

Lua apparently passes all parameters to functions by value. The value of a Lua table is a reference to the table, so tables can be altered by a function. Here is some code that demonstrates pass by value:

Lua
> t = {1, 2, 3}
> a = function(x) x[1] = 10 end
> a(t)
> = t[1]
10

> s = "hello"
> a = function(x) x = "jello" end
> a(s)
> print(s)
Hello

> g = function() print(10) end
> a = function(x) x = function() print(5) end end
> g()
10
> a(g)
> g()
10