Lua, like many other languages, the argument list is wrapped in parentheses when the function is called:
Copy Code code as follows:
In particular, if the function call has only one argument, and if this parameter is a string literal (literal) or table constructor (constructor), the parentheses of the wrapping parameter can be omitted:
Copy Code code as follows:
print ' Hello World ' <--> print (' Hello World ')
type{} <--> type ({})
Lua provides a special syntax for object-oriented invocation:
Copy Code code as follows:
O:foo (x) <--> O.foo (o, x)
The functions called by LUA may be defined in Lua or in C (all functions in the LUA standard library are written in C).
Definition of function
Copy Code code as follows:
function Add (a)
Local sum = 0
For i = 1, #a do
sum = sum + a[i]
End
return sum
End
Print (Add{1, 2, 3})
When a function is called, the number of arguments (arguments) and formal parameters (parameters) can be mismatched, the superfluous actual participants are discarded, and the extra parameter values are nil, for example:
Copy Code code as follows:
function f (A, B) print (A, b) end
F (3)--> 3 nil
F (3, 4)--> 3 4
F (3, 4, 5)--> 3 4
function multi-value return
In Lua, a function can return multiple values. For example:
Copy Code code as follows:
function Maximum (a)
Local mi = 1
Local m = A[mi]
For i = 1, #a do
If a[i] > m Then
Mi = i; m = A[i]
End
End
return m, MI
End
Print (Maximum{8, 10, 23, 12, 5})
When you assign more values, the extra values are discarded, and the value of the variable is nil:
Copy Code code as follows:
X, y = 1--> x = = 1, y = = Nil
X, y = 1, 2, 3--> x = = 1, y = = 2
This rule is followed on the value processing of the function call time parameter, and on the fetch of the function return value. For example:
Copy Code code as follows:
function Foo0 () end
function foo1 () return ' a ' end
function Foo2 () return ' A ', ' B ' end
X, y = Foo2 ()--> x = = ' A ', y = = ' B '
x = Foo2 ()--> x = = ' A '
X, y, z = ten, Foo2 ()--> x = =, y = = ' a ', z = = ' B '
t = {Foo2 ()}--> {' A ', ' B '}
Look at one more example:
Copy Code code as follows:
function Foo2 () return ' A ', ' B ' end
X, y = Foo2 (),--> x = = ' A ', y = = 20
Here, the function only provides a value because the function call is not at the last position in the list. A more meaningful example:
Copy Code code as follows:
function Foo2 () return ' A ', ' B ' end
Print (Foo2 ())--> a B
Print (Foo2 (), 1)--> a 1
If the function call is in the last position of the list, and the () Wrap function call is used, the function also provides only one value:
Copy Code code as follows:
function Foo2 () return ' A ', ' B ' end
Print (Foo2 ())--> a B
Print ((Foo2 ())--> a
A useful function of using the attribute returned by a function is Table.unpack, which takes an array as an argument and returns all the elements in the array:
Copy Code code as follows:
Print ({A,})--> table:00000000005bbe00
Print (TABLE.UNPACK{10,})--> 10 20 30
Variable length parameter
We can pass any number of arguments when we use the print function. Lua offers ... Represents the parameter list, let's implement a function like print:
Copy Code code as follows:
function Add (...)
Local s = 0
For _, V. in ipairs{...} do
s = s + V
End
return s
End
Print (Add (3, 4, 5))--> 12
One more example:
Copy Code code as follows:
function Test (...)
Local A, B = ...
Print (A, B)
End
Test (1, 2, 3)--> 1 2
There is also a special case where we need to note:
Copy Code code as follows:
function P (...)
For _, V. in ipairs{...} do
Print (v)
End
End
P (1, Nil, 3)--> 1
Print (1, nil, 3)--> 1 Nil 3
As we can see in the example above, our P function does not output the result according to our intention when it exists nil in the parameter. Lua provides a table.pack function to get its invocation parameters (including the nil arguments) and returns a table containing all the arguments, which has an extra field n that represents the number of parameters:
Copy Code code as follows:
function P (...)
Local arg = Table.pack (...)
For i = 1, ARG.N do
Print (Arg[i])
End
End
P (1, Nil, 3, nil)--> 1 Nil 3 nil
However, it is important to note that {...} is more efficient than table.pack (...), and we can use it to ensure that there are no nil parameters.
function is the first class value (first-class values)
We can use functions like other variables:
Copy Code code as follows:
A = {p = print}
A.P (' Hello World ')--> Hello World
print = Math.sin
A.P (print (1))--> 0.8414709848079
Similar to {} as a constructor for a table, we can assume that function (x) End is the constructor of a constructor:
Copy Code code as follows:
Local add = function (A, B)
Return a + b
End
Print (Add (1, 2))
--Another way of writing
Local function Add (A, B)
Return a + b
End
The Table.sort function is used for sorting, and it can accept a sort function as an argument:
Copy Code code as follows:
Network = {
{name = ' Grauna ', IP = ' 210.26.30.34 '},
{name = ' arraial ', IP = ' 210.26.30.23 '},
{name = "Lua", IP = "210.26.23.12"},
{name = ' Derain ', IP = ' 210.26.23.20 '},
}
Print ('--------------')
For _, V. in Ipairs (Network) do
Print (V.name)
End
Table.sort (Network, function (A, B)
return a.name > B.name
End
Print ('--------------')
For _, V. in Ipairs (Network) do
Print (V.name)
End
The output results are:
Copy Code code as follows:
--------------
Grauna
Arraial
Lua
Derain
--------------
Lua
Grauna
Derain
Arraial
In this example, the sort function we provide is passed to the Table.sort function as a parameter, and functions such as this sort function with no name are called anonymous functions.
Closure (closures)
Many languages support closures (Golang, JavaScript, etc.). A function and its access to external variables form a closure. Look at an example:
Copy Code code as follows:
function Newcounter ()
Local i = 0
return function ()
i = i + 1
return I
End
End
C1 = Newcounter ()
Print (C1 ())--> 1
Print (C1 ())--> 2
The C1 here is a closure (external variable i) that creates a closure (and creates a new variable i) each time the Newcounter is invoked:
Copy Code code as follows:
C2 = Newcounter ()
Print (C2 ())--> 1
Print (C1 ())--> 3
Print (C2 ())--> 2