Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua function


May 12, 2021 Lua


Table of contents


Lua function

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

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

Lua functions have two main uses:

  • 1. Complete the specified task, in which case the function is used as a call statement;
  • 2. Evaluate and return the value, in which case the function is used as an expression of the assignment statement.

The function definition

The Lua programming language function is defined in the following format:

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

Analytical:

  • optional_function_scope
  • : Is the argument optional whether the formulation function is a global function or a local function?
    This parameter is not set to the global function by default, and if you need to set the function to local, you need to use the keyword local.
  • function_name:
  • Specify the name of the function.
  • argument1, argument2, argument3..., argumentn:
  • Function parameters, multiple arguments separated by commas, functions can also be without arguments.
  • function_body:
  • The body of the function, the block of code statements that need to be executed in the function.
  • result_params_comma_separated:
  • The function returns a value, and the Lua language function can return multiple values, each separated by a comma.

    Instance

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

--[[ 函数返回两个值的最大值 --]]
function max(num1, num2)

   if (num1 > num2) then
      result = num1;
   else
      result = num2;
   end

   return result; 
end
-- 调用函数
print("两值比较最大值为 ",max(10,4))
print("两值比较最大值为 ",max(5,6))

The above code execution results are:

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

In Lua we can pass a function as an argument to the function, as follows:

myprint = function(param)
   print("这是打印函数 -   ##",param,"##")
end

function add(num1,num2,functionPrint)
   result = num1 + num2
   -- 调用传递的函数参数
   functionPrint(result)
end
myprint(10)
-- myprint 函数作为参数传递
add(2,5,myprint)

The above code execution results are:

这是打印函数 -   ##   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 subsector" (if there is no matching string return nil).

> s, e = string.find("www.w3cschool.cn", "w3cschool") 
> print(s, e)
5	13

In the Lua function, a list of worth returns can be returned after the return to 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
       end
    end
    return m, mi
end

print(maximum({8,10,23,12,5}))

The above code execution results are:

23   3

Variable parameters

The Lua function can accept a variable number of parameters, similar to the C language, which uses three points in the list of function parameters (...) Indicates that the function has variable parameters.

Lua places the parameters of the function in a table called #arg represents the number of incoming arguments.

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
   end
   print("总共传入 " .. #arg .. " 个数")
   return result/#arg
end

print("平均值为",average(10,5,3,4,5,6))

The above code execution results are:

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