There are two main uses of the Lua function:
1. Complete the specified task, in which case the function is used as the calling statement;
2. Calculates and returns a value, in which case the function is used as an expression of an assignment statement. function Definition
The Lua programming language functions are defined in the following format:
Optional_function_scope function function_name (argument1, argument2, Argument3 ..., argumentn)
function_body return
result_params_comma_separated
end
Optional_function_scope:
This parameter is optional, specifies whether the function is global or local, is not set this parameter defaults to global functions, local functions need to use the keyword。 Function_name:
Specifies the function name. Argument1, Argument2, Argument3 ..., ARGUMENTN:
function arguments, multiple arguments are separated by commas, and functions can be without parameters. Function_body:
function body, a block of code statements that need to be executed in a function. Result_params_comma_separated:
function return value,
can return multiple values, each value is separated by a comma.
instance
The following instance defines the function Max (), the parameter is NUM1, num2, is used to compare the size of two values, and returns the maximum value:
The--[[function returns the maximum value of two values-]]
function max (NUM1, num2)
if (Num1 > num2) then result
= NUM1;
else result
= num2;
End return result
;
End
--Call function
print ("Two values are compared to Max, Max (10,4)")
print ("Two values are the maximum value is", Max (5,6))
The above code execution results are:
The maximum value of two values is
two compared with the maximum value of 6.
in Lua we can pass functions as arguments to functions , as follows:
Myprint = function (param)
print ("This is the print function- # #", Param, "# #") End
function Add (num1,num2, Functionprint) Result
= Num1 + num2
--Call the passed function parameter
functionprint (result)
end
Myprint (10)
--myprint function as a parameter to pass
Add (2,5,myprint)
The above code execution results are:
This is the print function- # # #
This is the print function- # # 7 # #
Multiple return Values
The Lua function can return multiple result values , such as String.find, that return a matching string "Start and end subscript" (if no matching string returns nil).
> S, E = String.find ("www.runoob.com", "Runoob")
> Print (S, e)
5 10
In the LUA function, you can return multiple values by listing the list of worthwhile returns after return, such as:
function maximum (a) local
mi = 1 --Maximum index local
m = A[mi] --Maximum for
i,val in Ipairs (a) do
if Val > m then
mi = i
m = Val End return
m, MI end
print (maximum ({8,10,23,12,5}))
The above code execution results are:
3
variable Parameters
The Lua function can accept a variable number of arguments , similar to the C language, using three points in the function argument list ... Indicates that the function has variable parameters.
function Add (...)
The local s = 0
for I, V in ipairs{...} do --> {...} represents an array of all variable-length parameters
s = s + V-return
s
End
Print (Add (3,4,5,6,7)) --->25
We can assign a variable parameter to a variable.
For example, we calculate the average of several numbers:
function average (...)
result = 0 Local
arg={...} --> Arg is a table, local variable for
i,v in Ipairs (ARG) does result
= result + v
-end
print ("Incoming" in total). #arg. "Number") return
result/#arg
end
print ("average", average (10,5,3,4,5,6))
The above code execution results are:
A total of 6 numbers
to the average of 5.5
We can also get the number of variable parameters by select ("#",...):
function average (...)
result = 0 Local
arg={...}
For i,v in Ipairs (ARG) does result
= result + v
-end
Print ("Total incoming" ...) Select ("#",...). "Number") return
Result/select ("#",...)
End
Print ("average", average (10,5,3,4,5,6))
The above code execution results are:
A total of 6 numbers
to the average of 5.5
Sometimes we may need several fixed parameters plus variable parameters, and the fixed arguments must be placed before the variable length parameter:
function fwrite (FMT, ...) ---> Fixed parameters fmt return
io.write (String.Format (FMT, ...))
End
fwrite ("runoob\n") --->fmt = "runoob" with no variable length parameters.
fwrite ("%d%d\n", 1, 2) --->fmt = "%d%d", variable length parameters 1 and 2
The output results are:
Runoob
12
Typically, when traversing variable-length parameters, you only need to use {..}, while variable-length parameters may contain some nil, you can use the Select function to access variable-length parameters: Select (' # ', ...) or select (N, ...)
Select (' # ', ...) returns the length of the variable parameter
Select (n, ...) to access parameters for N to select (' # ',...)
When you call Select, you must pass in a fixed argument selector (selector switch) and a series of variable-length parameters. If selector is a number n, then select returns its nth variable argument, otherwise it can only be the string "#", so that the select returns the total number of variable-length arguments. Example code:
Do
function foo (...)
For i = 1, select (' # ', ...) do --> get the total number of parameters local
arg = select (I, ...);--> read parameter
print ("arg", ARG);
End
End
foo (1, 2, 3, 4);
End
The output results are:
arg 1 arg 2 arg 3 arg 4