Introduction
In Lua, a variable must be declared in code before it is used, that is, the variable is created. This is because the compiler needs to know how to open a store for a statement variable before the compiler executes the code to store the value of the variable. Variable Type
There are three types of variables in LUA: Global variables, local variables, and fields in the table.
The variables in LUA are all global variables, even in blocks of statements or in functions, unless declared as local variables with the local display. Scope of the variable
A local variable is scoped to the end of the statement block from the start of the declaration position. When another module is called, only the global variables that are in that block of the statement cannot access the local variables inside.
The default value for a variable is nil.
Code Show
A = 5 --global variable local
b = 5 --local variable
function joke ()
C = 5 --global variable local
d = 6 --local variable
end
joke ()
print (c,d)- 5 nil
do
local A = 6 --local variable
b = 6 --Global variable
Print (A, b);
--6 6 End
print (A, b) and 5 6
Perform the above instance output as: