Lua function


Release date:2023-09-29 Update date:2023-10-13 Editor:admin View counts:275

Label:

Lua function

In Lua, functions are the main way to abstract statements and expressions. It can not only be used to handle some special work, but also can be used tocalculate some values.

Lua provides many built-in functions that you can easily call in your program, such as print() function can print the passed parameters on the console.

The Lua function has two main uses:

  • 1.Complete the specified task, in which case the function is used as a calling statement;

  • 2.Calculate and return a value, in which case the function is used as an expression for the assignment statement.

Function definition

The Lua programming language function definition format is as follows:

optional_function_scope function function_name( argument1, argument2, argument3..., argumentn)
    function_body
    return result_params_comma_separated
end

Parsing:

  • optional_function_scope : Whether this parameter is optional determines the function is a global function or a local function. If this parameter is not set, it defaults to the global function. If you need to setthe function to a local function, you need to use keywords. local .

  • function_name : Specify the function name.

  • argument1, argument2, argument3..., argumentn : function parameters, multiple parameters are separated by commas, the function can also have no parameters.

  • f``unction _ body``: the body of the function, the block of code statements to be executed in the function.

  • result_params_comma_separated : Function returns values, Lua language functions can return multiple values, each separated by a comma.

Powered by TorCMS (https://github.com/bukun/TorCMS).