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) function_body return result_params_comma_separated end
Analysis:
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; else result = num2; end return result; end -- 调用函数 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,"##") end function add(num1,num2,functionPrint) result = num1 + num2 -- 调用传递的函数参数 functionPrint(result) end myprint(10) -- myprint 函数作为参数传递 add(2,5,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("www.php.cn", "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 end end return m, mi end print(maximum({8,10,23,12,5}))
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 end print("总共传入 " .. #arg .. " 个数") return result/#arg end print("平均值为",average(10,5,3,4,5,6))
The execution result of the above code is:
总共传入 6 个数 平均值为 5.5