Endless Tutorial - Lua - Variable Declaration

Variable names can consist of letters, numbers and the underscore character. It must start with a letter or an underscore, since Lua is case sensitive, uppercase and lowercase letters are different.

In Lua, although there is no variable data type in the Wuya Tutorial, there are three types according to the scope of the variable.

  • Global Variables (Global) - Unless explicitly declared as local variables, all variables are considered global variables.

  • Local Variable (Local) - When the type is specified as a local variable of a variable, its scope is limited by the function.

  • Table field − This is a special type of variable that can hold anything but nil, including functions.

Lua variable definition

A variable definition means telling the interpreter where and how much storage to create for the variable. A variable definition has an optional type and contains a list of one or more variables of that type, as follows:

type variable_list;

Here, type can be local or a specified type, making it global, and variable_list can consist of one or more identifier names separated by commas.

local    i, j
local    i
local    a,c

The local i,j line declares and defines both the variables i and j; it instructs the interpreter to create variables named i,j and restrict the scope to local.

Variables can be initialized in their declaration. An initializer consists of an equals sign and a constant expression, as follows:

type variable_list=value_list;

Some examples are -

local d , f=5 ,10     --declaration of d and f as local variables. 
d , f=5, 10;          --declaration of d and f as global variables. 
d, f=10               --[[declaration of d and f as global variables. 
                           Here value of f is nil --]]

Lua variable declaration

As you can see in the example above, the assignment of multiple variables follows the variable_list and value_list formats. In the above example local d, f=5, 10 , we have d and f in the variable_list and 5 and 10 in the value list.

Value assignment in Lua works like the first variable in variable_list with the first value in value_list, and so on. Therefore, the value of d is 5 and the value of f is 10.

Try the following example, where variables are declared at the top, but defined and initialized in the main function -

-- Variable definition:
local a, b

-- Initialization
a=10
b=30

print("value of a:", a)

print("value of b:", b)

-- Swapping of variables
b, a=a, b

print("value of a:", a)

print("value of b:", b)

f=70.0/3.0
print("value of f", f)

After building and executing the above code, the following output will be produced −

value of a:	10
value of b:	30
value of a:	30
value of b:	10
value of f	23.333333333333

Lua - Variable Declaration - Wuya Tutorial Network Wuya Tutorial Network provides variable names that can consist of letters, numbers and underscore characters. It must start with a letter or underscore, due to the Lua zone... https://www.learnfk.com/lua/lua-variables.html

Guess you like

Origin blog.csdn.net/w116858389/article/details/132040730