• 函数的参数
    • 按值传递
    • 变长参数
    • 具名参数
    • 按引用传递

    函数的参数

    按值传递

    Lua 函数的参数大部分是按值传递的。值传递就是调用函数时,实参把它的值通过赋值运算传递给形参,然后形参的改变和实参就没有关系了。在这个过程中,实参是通过它在参数表中的位置与形参匹配起来的。

    示例代码:

    1. local function swap(a, b) --定义函数swap,函数内部进行交换两个变量的值
    2. local temp = a
    3. a = b
    4. b = temp
    5. print(a, b)
    6. end
    7. local x = "hello"
    8. local y = 20
    9. print(x, y)
    10. swap(x, y) --调用swap函数
    11. print(x, y) --调用swap函数后,xy的值并没有交换
    12. -->output
    13. hello 20
    14. 20 hello
    15. hello 20

    在调用函数的时候,若形参个数和实参个数不同时,Lua 会自动调整实参个数。调整规则:若实参个数大于形参个数,从左向右,多余的实参被忽略;若实参个数小于形参个数,从左向右,没有被实参初始化的形参会被初始化为 nil。

    示例代码:

    1. local function fun1(a, b) --两个形参,多余的实参被忽略掉
    2. print(a, b)
    3. end
    4. local function fun2(a, b, c, d) --四个形参,没有被实参初始化的形参,用nil初始化
    5. print(a, b, c, d)
    6. end
    7. local x = 1
    8. local y = 2
    9. local z = 3
    10. fun1(x, y, z) -- z被函数fun1忽略掉了,参数变成 x, y
    11. fun2(x, y, z) -- 后面自动加上一个nil,参数变成 x, y, z, nil
    12. -->output
    13. 1 2
    14. 1 2 3 nil

    变长参数

    上面函数的参数都是固定的,其实 Lua 还支持变长参数。若形参为 ... , 表示该函数可以接收不同长度的参数。访问参数的时候也要使用 ...

    示例代码:

    1. local function func( ... ) -- 形参为 ... ,表示函数采用变长参数
    2. local temp = {...} -- 访问的时候也要使用 ...
    3. local ans = table.concat(temp, " ") -- 使用 table.concat 库函数对数
    4. -- 组内容使用 " " 拼接成字符串。
    5. print(ans)
    6. end
    7. func(1, 2) -- 传递了两个参数
    8. func(1, 2, 3, 4) -- 传递了四个参数
    9. -->output
    10. 1 2
    11. 1 2 3 4

    值得一提的是,LuaJIT 2 尚不能 JIT 编译这种变长参数的用法,只能解释执行。所以对性能敏感的代码,应当避免使用此种形式。

    具名参数

    Lua 还支持通过名称来指定实参,这时候要把所有的实参组织到一个 table 中,并将这个 table 作为唯一的实参传给函数。

    示例代码:

    1. local function change(arg) -- change 函数,改变长方形的长和宽,使其各增长一倍
    2. arg.width = arg.width * 2
    3. arg.height = arg.height * 2
    4. return arg
    5. end
    6. local rectangle = { width = 20, height = 15 }
    7. print("before change:", "width =", rectangle.width,
    8. "height =", rectangle.height)
    9. rectangle = change(rectangle)
    10. print("after change:", "width =", rectangle.width,
    11. "height =", rectangle.height)
    12. -->output
    13. before change: width = 20 height = 15
    14. after change: width = 40 height = 30

    按引用传递

    当函数参数是 table 类型时,传递进来的是 实际参数的引用,此时在函数内部对该 table 所做的修改,会直接对调用者所传递的实际参数生效,而无需自己返回结果和让调用者进行赋值。
    我们把上面改变长方形长和宽的例子修改一下。

    示例代码:

    1. function change(arg) --change函数,改变长方形的长和宽,使其各增长一倍
    2. arg.width = arg.width * 2 --表arg不是表rectangle的拷贝,他们是同一个表
    3. arg.height = arg.height * 2
    4. end -- 没有return语句了
    5. local rectangle = { width = 20, height = 15 }
    6. print("before change:", "width = ", rectangle.width,
    7. " height = ", rectangle.height)
    8. change(rectangle)
    9. print("after change:", "width = ", rectangle.width,
    10. " height =", rectangle.height)
    11. --> output
    12. before change: width = 20 height = 15
    13. after change: width = 40 height = 30

    在常用基本类型中,除了 table 是按址传递类型外,其它的都是按值传递参数。
    用全局变量来代替函数参数的不好编程习惯应该被抵制,良好的编程习惯应该是减少全局变量的使用。