Programming in lua translation --functions

xiaoxiao2021-03-06  38

5.Functions functions There are two uses: 1. Complete the specified task, in which the function is used as the call statement 2. Calculates and returns the value, this is the function as an expression of the assignment statement. Syntax: func_name Arguments-list; end; When the function is called, if the parameter list is empty, you must use () indicate whether the function call. Print (8 * 9, 9/8) a = math.sin (3) Math .COS (10) Print (Os.Date ()) The above rule has an exception, when the function is only one parameter and this parameter is a string or a table configuration, () is optional: Print "Hello World" <- -> Print ("Hello World" Dofile 'a.lua' <-> Dofile ('a.lua') Print [[A Multi-line <-> Print ([[A Multi-line Message]] Message ]]) F {x = 10, y = 20} <-> f ({x = 10, y = 20}) TYPE {} ​​<-> type ({}) Lua also provides an object-oriented mode call function. Syntax, such as O: Foo (X) and O. Foo (O, X) are equivalent, the following chapters will detail the object-oriented content. The function used by Lua can be Lua writing or other languages. For other languages, LUA programmers use what language implemented function. The same. LUA function is similar to the matching statement of the parameter and the parameter, the excess part is ignored, and the other part is ignored. Function F (A, B) Return A OR B End Call Parameters F (3) A = 3, B = NIL F (3, 4) A = 3, B = 4 f (3, 4, 5) A = 3, B = 4 (5 IS Discarded) 5.1 Returns multiple result value Lua functions to return multiple result values, some predefined functions returns a multi-value such as String.Find, he returns the beginning and end of the matching string (if there is no match String returns NIL). S, e = string.find ("Hello Lua Users", "Lua") Print (s, e) -> 7 9lua function, list the list of denoted by returning to return, Multi-values, such as: function maximum (a) local mi = 1 - Maximum Index local m = a [Mi] - Maximum Value for i, val in ipairs (a) do if Val> mi = i m = VAL End End Return M, MI End Print (Maximum ({8, 10, 23, 12, 5}) -> 23 3lua always adjusts the number of function returned values ​​to apply the calling environment, when calling the function as a statement All return values ​​are ignored. Function Foo0 () end - returns no results function foo1 () return 'a'

END - RETURns 1 Result FUNCTION FOO2 () Return 'a', 'B' end - returns 2 results First, when the function is called as an expression: there are several cases: 1. When the call is used as the last one of the expression When the parameter or only one parameter, the number of values ​​according to the variable is as much as possible, it is not enough to make up the NIL, beyond the house. 2. In other cases, the function call returns only the first value (if there is no return value) NIL) x, y = foo2 () - x = 'a', y = 'b' x = foo2 () - x = 'a', 'b' IS discarded x, y, z = 10, foo2 ( ) - x = 10, y = 'a', z = 'b' x, y = fo0 () - x = nil, y = nil x, y = foo1 () - x = 'a', y = NIL X, Y, Z = foo2 () - x = 'a', y = 'b', z = nil x, y = foo2 (), 20 - x = 'a', y = 20 x, Y = foo0 (), 20, 30 - x = 'nil', y = 20, 30 IS discarded second, function call is the same as the function parameter is called, and the multi-value assignment is the same. Print (foo0 ()) - -> Print (foo1 ()) -> a print (foo2 ()) -> a b print (foo2 (), 1) -> a 1 print (foo2 () .. "x") -> AX third, function call is initialized in the table constructor, and the multi-value assignment is the same. A = {FOO0 ()} - a = {} (an Empty Table) A = {Foo1 ()} - a = {'a'} a = {foo2 ()} - a = {'a', 'b'} a = {foo0 (), foo2 (), 4} - a [1] = nil, a [2 ] = 'a', a [3] = 4 In addition, return f () This type of return F () returns All values ​​FUNCTION FOO (i) if i == 0 Then Return Foo0 () elseif i == 1 Then Return Foo1 () elseif i == 2 Then Return Foo2 () End Print (foo (1)) -> a print (foo (2)) -> a b print (no results) Print (no results) - (NO RESULTS) can use parentheses to enhance the call back a value. Print ((FOO0 ())) -> NIL Print ((foo1 ())) -> a print ((foo2 ())) ->

A A RETURN statement If you use parentheses to enclose the return value will result in return a value. The multi-value returned special function unpack accepts an array as an input parameter, returns all elements of the array. Enpack is used to implement a modal call Mechanism, in the C language, you can use the function pointer to call the variable function, which can declare the variable function, but can not be able to change at the same time. If you want to call the variable parameters in Lua, you only need this: f (unpack (a)) unpack Returns all elements as F () parameters f = string.find a = {"Hello", "LL"} Print (f (unpack (a)) -> 3 4 The predefined Unpack function is implemented in C language, we can also use Lua to complete: Function Unpack (t, i) i = i or 1 if t [i] Then Return T [I], Unpack (t, i 1) END END 5.2 Variable parameter Lua function can accept variable parameters, and C language Similar to the three-point (...) indicating that the function has variable parameters in the function parameter list. Lua puts the function's parameters In a table called Arg, in addition to the parameters, there is a domain n represent the number of parameters in the arg table. For example, we can rewrite the Print function: PrintResult = "" Function Print (...) for i, v in iPairs (arg) do printresult = printResult .. toString (v) .. "/ t" end printresult = printResult .. "/ n" End Sometimes we may need several fixed parameters plus variable parameters Function G (A, B, ...) End Call Parameters G (3) A = 3, B = NIL, Arg = {n = 0} g (3, 4) a = 3, b = 4, arg = {n = 0} g (3, 4, 5, 8) a = 3, b = 4, arg = {5, 8; n = 2} As shown above, Lua will transmit the front elements to the fixed parameters of the function, the back Place in the arg table. For a specific example, if we only want string.find to return Two values: A typical method is to use virtual variable (underscore) local _, x = string.find (s, p) - count USE `X '... can also declare a SELECT function using variable parameters: function SELECT (N, ...) RETURN ARG [N] End Print (String.Find ("Hello Hello", "Hel") -> 6 9 Print (SELECT (1, String.Find ("Hello Hello", "HEL"))) -> 6 Print (SELECT (2, String.Find ("Hello Hello", "Hel")))) ->

9 Sometimes you need to pass the variable parameters of the function to additional function calls, you can use the unpack (arg) we said before returning all variable parameters all of the Arg table, Lua provides a text-style function string.format. Similar to a C language Sprintf function): Function FWRITE (FMT, ...) Return IO.WRITE (String.Format (FMT, Unpack (arg))) End This example will be a function of the text. 5.3 Naming Parameters LUA's function parameters are related to the location, call the censoon in sequence, sometimes specifying the parameters in order, such as the rename function is used to rename a file, sometimes We don't know the order of the two parameters before and after the name: - Invalid code rename (Old = "Temp.lua", new = "temp1.lua") The above code is invalid, Lua can pass all The parameters are placed in a table, and the table is used as the unique parameters of the function to implement the above pseudo code. Because the LUA syntax support function calls, the actors can be the structure of the table. Rename {Old = "Temp.lua", New = "Temp1.lua"} We will redefine Rename: Function Rename (arg) Rename: Function Rename (arg.old, arg.new) based on this idea, this function parameter is passed when the function is used. It is very convenient. For example, the function created in the GUI library has many parameters and most of the parameters are optional. You can use the following way: w = window {x = 0, y = 0, width = 300, Height = 200, title = "lua", background = "blue", border = true} Function window (options) - check mandatory options = "String" The Error ("no title" Elseif Type (Options.width) ~ = "Number" Then Error (" ")") Elseif Type (options.Height) ~ = "Number" The Error ("No Height") end - everyhes else is optional _window (options.title, options.x or 0, - default value options.y OR 0, - Default Value Options.width, Options.Height, Options.Background Or "White", - Default Options.Border - Default is false (nil) End Author BLOG:

http://blog.9cbs.net/ouyangbuxiu/

转载请注明原文地址:https://www.9cbs.com/read-69184.html

New Post(0)