Purpose of the function:
1. Complete the specified task, which 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.
parameter of the function is empty, you must use () to represent the function call. Exception: () is optional when the function has only one argument, and this parameter is a string or table construct.
LUA provides syntax for object-oriented call functions
O:foo (x) is equivalent to O.F (o,x).
The matching of the real participation parameters of the LUA function is similar to the assignment statement, the redundant part is ignored, and the missing part nil is partially replenished.
The LUA function can return multiple values, and returns a list of values to return after returning a multivalued
Copy Code code as follows:
S, E = String.find ("Hello Lua", "LUA")
So how does Lua adjust the number of function return values to accommodate the calling environment?
1. Use as statement, so the return value is ignored.
2. Use as Expression
1. As the last parameter of the expression or only one parameter, according to the number of variables, the function to return as many values as possible, insufficient to fill nil, beyond the shed.
2). Other conditions the function call returns the first value (if no return value is nil)
3). When a function call is called as a function argument, it is the same as a multivalued assignment
4. When the function call is initialized in the table construct, it has the same value as the multivalued assignment
5). return f (); F () returns all values. You can use parentheses to force back a value return (f ())
C, you can use function pointers to call variable functions, you can declare variable functions, but not both.
LUA can invoke variable functions of variable parameters like this: F (unpack (a))
Unpack accepts an array as an input parameter, returning all elements of the array. Note is the number that returns the Ipair traversal result
Copy Code code as follows:
f = string.find
A = {"Hello lua", "Lua"}
Print (f (unpack (a))
Variable parameter use ... Indicates that LUA places the variable parameters in the table of ARG, containing a field n representing the number of parameters
(5.1+: to use; Instead of Arg. Such as:
Copy Code code as follows:
function Printarglen (...)
Print (SELECT ("#", ...))
End
)
function select (n, ...)
return Arg[n]
End
Name the function parameter: You can implement the function of named parameters by taking the table as a unique parameter to the function.
Copy Code code as follows:
Rename {old= "Temp.lua", new= "Temp1.lua"}
function Rename (ARG)
Os.rename (Arg.old, Arg.new)
End
Named arguments can be used when the arguments are more or less confusing.
Functions in Lua have the first class value of the word legal world.
First class value: In Lua, functions and other values (numeric, string), functions can be stored in a variable, also in a table, can be used as a function of parameters, but also as a function of the return value.
The word legal world: a nested function can access the variables of his external function.
The LUA function can also be anonymous, and when we refer to the function name, we actually say a variable that points to the function.
Closed Bag
Lexical definition plus the first class function is a powerful concept.
Copy Code code as follows:
function Sortbygrade (names, grades)
Table.sort (names, function (N1,N2)
return GRADES[N1]>GRADES[N2]
End
End
Inside an anonymous function grades is called an external local variable or upvalue
Closures provide useful functionality in the context of the environment. Can be used as an advanced function parameter, as a function nesting function (such as returning a closure), can be used as a callback function. A closure allows you to redefine a function, and when someone calls a function, it invokes a function that has been defined as a security check, creating a secure environment (sandbox)
Copy Code code as follows:
Todo
Local Oldopen = Io.open
Io.open = function (f,m)
If ACCESS_OK (f,m) Then
Return Oldopen (F,M)
Else
Return nil, "Access Denied"
End
End
End
Non-global functions
function as a table's domain (most LUA library functions are implemented using this mechanism.) such as Io.read,math.sin)
There are 3 kinds of grammatical methods:
1.
Copy Code code as follows:
Lib = {}
Lib.foo = function (x,y) return X+y end
2.
Copy Code code as follows:
Lib = {foo = function (x,y) return X+y End}
3.
Copy Code code as follows:
Lib = {}
function Lib.foo (x,y)
Return X+y
End
Finish