Latest web development tutorials

Lua function

In Lua, function is the main method of abstract statements and expressions. Both can be used to deal with some special work, it can also be used to calculate some values.

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

Lua function There are two main purposes:

  • 1. complete the assigned task, in this case is used as a function call statement;
  • 2. Calculate and returns the value, in which case the function is used as an assignment expression.

Function definition

Lua programming language functions defined in the following format:

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


  • optional_function_scope
  • : This parameter is optional function is to develop a global function or a local function, the end of the parameter is not set to a global function, if you need to set function is a partial function needs to use the keyword local.
  • function_name:
  • Specify the function name.
  • argument1, argument2, argument3 ..., argumentn :
  • Function parameters, multiple parameters separated by commas, can also function without parameters.
  • function_body:
  • Body of the function, function block code statements need to be performed.
  • result_params_comma_separated:
  • Function return values, Lua language function can return multiple values, each separated by commas.


    The following example defines a functionmax (), parameters num1, num2, used to compare the size of two values and returns 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 above code is executed as a result of:

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

    Lua we can function as a parameter passed to the function, the following examples:

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

    The above code is executed as a result of:

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

    Multiple return values

    Lua function can return multiple result values, such as string.find, which returns the matching string "beginning and end of the subscript" (if there is no match string returns nil).

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

    Lua function, after the return is worth to return a list of lists can return multiple values, 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 above code is executed as a result of:

    23	3

    Variable parameter

    Lua function can accept a variable number of arguments, and C language similar to using a three-point function parameter list (...) denotes a function with variable parameters.

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

    For example, we calculate the average of a few numbers:

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

    The above code is executed as a result of:

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