Lua functions

In Lua, functions are the main method of abstracting statements and expressions. It can be used to handle some special tasks or calculate some values.

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

Lua functions have two main uses:

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

  • 2. Calculate and return the value. In this case, the function is used as the expression of 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)
	return result_params_comma_separated


  • optional_function_scope

  • : This parameter is optional to specify whether the function is a global function or a local function. If this parameter is not set, the end is a global function. If you need to set the function to be local The function requires the keyword

  • local
  • .

  • function_name:

  • Specify the function name.

  • argument1, argument2, argument3..., argumentn:

  • Function parameters, multiple parameters are separated by commas On, the function can also take no parameters.

  • function_body:

  • Function body, the code statement block that needs to be executed in the function.

  • result_params_comma_separated:

  • Function return value, Lua language function can return multiple values, each value is separated by a comma separated.

  • Example

    The following example defines the function max(), with parameters num1 and num2, which is used to compare the size of two values ​​and return the maximum value:

    --[[ 函数返回两个值的最大值 --]]
    function max(num1, num2)
       if (num1 > num2) then
          result = num1;
          result = num2;
       return result; 
    -- 调用函数
    print("两值比较最大值为 ",max(10,4))
    print("两值比较最大值为 ",max(5,6))

    The execution result of the above code is:

    两值比较最大值为 	10
    两值比较最大值为 	6

    In Lua, we can pass the function as a parameter to the function, as shown in the following example:

    myprint = function(param)
       print("这是打印函数 -   ##",param,"##")
    function add(num1,num2,functionPrint)
       result = num1 + num2
       -- 调用传递的函数参数
    -- myprint 函数作为参数传递

    The execution result of the above code is:

    这是打印函数 -   ##	10	##
    这是打印函数 -   ##	7	##

    Multiple return values

    The Lua function can return multiple result values, such as string.find, which returns the matching string "start and end subscripts" (if there is no matching string, return nil).

    > s, e = string.find("", "php") 
    > print(s, e)
    5	10

    In the Lua function, multiple values ​​can be returned by listing the list of values ​​to be returned after return, such as:

    function maximum (a)
        local mi = 1             -- 最大值索引
        local m = a[mi]          -- 最大值
        for i,val in ipairs(a) do
           if val > m then
               mi = i
               m = val
        return m, mi

    The execution result of the above code is:

    23	3

    Variable parameters

    The Lua function can accept a variable number of parameters. Similar to the C language, three dots (...) are used in the function parameter list to indicate that the function has variable parameters.

    Lua places the parameters of the function in a table called arg, #arg represents the number of parameters passed in.

    For example, we calculate the average of several numbers:

    function average(...)
       result = 0
       local arg={...}
       for i,v in ipairs(arg) do
          result = result + v
       print("总共传入 " .. #arg .. " 个数")
       return result/#arg

    The execution result of the above code is:

    总共传入 6 个数
    平均值为	5.5