Lua learning notes function
1. Functions
The function mainly completes the specified task. In this case, the function is used as a call statement, and the function can be computed and returned. In this case, the function is used as the expression of the value assignment statement.
Syntax:
Funcationfunc_name (arguments-list)
Statements-list
End
When calling a function, if the parameter list is empty, you must use () to call the function.
Print (8*9/8)
A = math. sin (3) + math. cos (10)
Print (OS. date ())
The preceding rule has an exception. When a function has only one parameter and the parameter is string or table constructed, () is optional:
Print "hello world" <--> print ("hello world ")
Dofile 'A. Lua' <--> dofile ('A. Lua ')
The matching of the lua function real parameters and form parameters is similar to that of the value assignment statement. The excess parameters are ignored, and the missing parameters are supplemented by nil.
2. Multiple result values are returned.
The Lua function can return multiple result values, for example, strng. find. It returns the matched string "subscript of start and end" (if no match string exists, nil is returned)
S, e = string. find ("hello lua users", "lua ")
Print (s, e) --> 7 9
In the lua function, multiple values can be returned after listing the values to be returned to the list, for example:
Function maximum ()
Local mi = 1
Local m = a [mi]
For I, val inipairs (a) do
Ifval> m then
Mi = I
M = val
End
Return m, mi
End
Print (maxmun ({, 12, 5}) --> 23 3
3. Variable parameters
The Lua function can accept variable parameters. Similar to the C language, it uses three points (…) in the function parameter list (...) It indicates that the function has variable parameters. Lua places the function parameters in a table named arg. In addition to parameters, the arg table also has a field n to indicate the number of parameters.
4. Name parameters
The Lua function parameters are location-related. The real parameters are passed to the form parameters in sequence during the call. It is useful to specify parameters by name. For example, the rename function is used to rename a file. Sometimes we cannot remember the order of the two parameters before and after the name:
Rename (old = "temp. lua", new = "temp1.lua ")
The above code is invalid. lua can put all the parameters in a table and use the table as the unique parameter of the function to implement the above pseudo code function, because the lua syntax supports function calling, real parameters can be table structures.
Rename {old = "temp. lua", new = "temp1.lua "}
Based on this idea, we have redefined rename:
Functionrename (arg)
Returnos. rename (arg. old, arg. new)
End
5. Closure
When a function is nested with another function definition, the internal function body can access the local variables of external functions. This special feature is called lexical demarcation. Although this seems clear, this is not the case:
If there is a list of student names and a table used for student names and scores: sort students according to their scores from high to low, you can do this:
Names = {"peter", "paul", "mary "}
Grades = {mary = 10, paul = 7, peter = 8}
Table. sort (Names, function (n1, n2 ))
ReturnGrades [n1]> Grades [n2]
End)
Create a function to implement this function
Function sortbygrade (Names, Grades)
Table. sort (Names, function (n1, n2 ))
ReturnGrades [n1]> Grades [n2]
End)
End
In this example, the anonymous function in the sort function inside the sortbygrade function can access the parameter Grades of sortbygrade. In the anonymous function, Grades is neither a global variable nor a local variable. It is called an external local variable.
See the following code:
FunctionnewCounter ()
Locali = 0
Returnfunction () -- anonymous function
I = I + 1 -- External local variables
Return I
End
End
C1 = newCounter ()
Print (c1 () --> 1
Print (c1 () --> 2
Anonymous functions use external local variables to save their counts. When we call an anonymous function, I is out of scope, because the newCounter function that created I has returned, then Lua correctly handled the situation using the closure idea. To put it simply, a closure is a function plus it can correctly access external local variables. If newCounter is called again, a new local variable I will be created, so we get a new closure that works on the new variable I.
Technically, a closure refers to a value rather than a function. A function is just a prototype declaration of a closure.
Lua learning notes function