# function

# Function definition

You can define anonymous functions, you can also define named functions (but you cannot define global function names), and you can also create closures

such as

let abc = function(n: number) 
        return n + 1
let function add2(n: number)
    return n+2

let M = {}
function M:sayHi()
    if 2 < 1 then

There can be a return statement in the code block of the function, which means that 0 or one value is returned to the caller, or there is no return statement. But after the return statement and the same conditional branch, this function body cannot have other statements

such as

let abc = function (n: number)
    return n+1
    pprint(n)             -- Compile errors here, there should be no other statements after the return statement

The parameter declaration of a function can be accompanied by a parameter type declaration, the syntax is as follows: (name: string, age: int), the compile-time type of this parameter is the object type without the type declaration

More simplified function expressions

The syntax of the above function definition can also define anonymous functions, but sometimes some simple anonymous functions are not convenient to write, so a more simplified function expression syntax is provided

  • Single expression function

The function body can only be a single expression, and it must be on the same line as the function parameter

The grammar rules are as follows

Args => Expr

such as

let a1 = (a: number, b: number) => a + b  -- a1 type is (number, number) => number
let a2 = a1(1, 2) -- result is 3
  • Multi-line expression functions

The function body can be multiple statements, and it is not restricted to be on the same line

The grammar rules are as follows

Args => do

such as

let a3 = (a: number, b: number) => do  
    pprint(a + b)
    return a + b
end -- a3 type is (number, number) => number
let a4 = a3(1, 2) -- Output 3 and set a4 to 3

# Function call

  • There are two syntaxes for function calls. One is a function object or variable followed by (), which can be passed in several parameters, such as

    var result = sayHi('glua', 123)

  • Another function call syntax is that if the function call has only one parameter and this parameter is a string literal or table literal, you can omit the parentheses here, such as

    var result = print 'hello' var result2 = pprint {hello: 2}

  • If the return value is returned in the function call, it will be returned to the caller. For example, in the above example, the return value is assigned to the result.

  • The function may have return statements in different branches of the function body code, and the first return value will be returned according to the actual running result. The compile time will return the compile-time type of the value of each return statement to analyze the return type of this function by union analysis. such as:

    let function hello(n: number) -- Because the return value of each return statement in the function body of this function is int, string, int, so the return type of this function is int | string if n > 10 then return n -- This return statement returns type int elseif n > 3 then return 'hello' -- This return statement returns the string type else return 0 -- This return statement returns type int end end

  • There is a syntax for function definition, such as function M:init() print(self.name) end. This colon syntax is equivalent to a dot, but it automatically adds a self parameter to indicate an M object. It is similar when calling such a function. , M:init() is called, and the M object is automatically placed as the self parameter into the first parameter of the M.init function at runtime. However, it should be noted that the API function definition of the contract only supports colon syntax. such as

    var t1 = {name: 'glua'} function t1:sayHello()

  • When the function is called, the parameter type of the function definition needs to be the same as the type of the variable or value used in the actual call, otherwise an error will be reported