Lua Functions
In Lua, functions are the main method of abstracting statements and expressions. They can be used to handle some special tasks, and can also be used to calculate some values.
Lua provides many built-in functions that you can conveniently call in your programs. For example, the print() function can print the passed arguments to the console.
Lua functions have two main uses:
- 1. Complete a specified task; in this case the function is used as a call statement;
- 2. Calculate and return values; in this case the function is used as an expression in an assignment statement.
Function definition
The function definition format in the Lua programming language is as follows:
optional_function_scope function function_name( argument1, argument2, argument3..., argumentn)
function_body
return result_params_comma_separated
end
Analysis:
-
optional_function_scope: This parameter optionally specifies whether the function is a global function or a local function. If not set, the function defaults to a global function. If you need to set the function as a local function, you need to use the keyword local。
-
function_name: Specify the function name.
-
argument1, argument2, argument3..., argumentn: Function parameters; multiple parameters are separated by commas, and a function can also have no parameters.
-
function_body: The function body, the block of code statements to be executed in the function.
-
result_params_comma_separated: Function return values. Lua language functions can return multiple values, each separated by a comma.