Lua中的函数和C++中的函数的含义是一致的,Lua中的函数格式如下:
1
2
3
|
在调用函数时,也需要将对应的参数放在一对圆括号中,即使调用函数时没有参数,也必须写出一对空括号。对于这个规则只有一种特殊的例外情况:一个函数若只有一个参数,并且此参数是一个字符串或table构造式,那么圆括号便可以省略掉。看以下代码:
上面代码的一些简便写法,如果不熟悉的话,在阅读别人的代码时,就会是一头雾水。
一个函数定义具有一个名称、一系列的参数和一个函数体。函数定义时,所定义的参数的使用方式与局部变量非常相似,它们是由调用函数时的“实际参数”初始化的。调用函数时提供的实参数量可以与形参数量不同。Lua会自动调整实参的数量,以匹配参数表的要求,若“实参多余形参,则舍弃多余的实参;若实参不足,则多余的形参初始化为nil”。这个与接下来要介绍的多重返回值非常相似。
多重返回值
这个应该是Lua的一个特征吧。允许函数返回多个结果,只需要在return关键字后列出所有的返回值即可。以下根据带来来说明情况:
function foo0() end -- 无返回值
function foo2()
"a"
,
"b"
end -- 返回两个结果
-- 那么Lua会保留其尽可能多的返回值,用于匹配赋值变量
x, y = foo2() -- x =
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y =
"b"
x = foo2() -- x =
"b"
被丢弃
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y, z = 10, foo2() -- x = 10, z =
"b"
-- nil来补充缺失的值
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = foo0() -- x = nil, y = nil
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = foo1() -- x =
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = nil
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, z = foo2() -- x =
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, z = nil
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = foo2(), 20 -- x =
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = 20
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, y = foo0(), 20, 30 -- x = nil, y = 20, 30则被丢弃
-- 方面的调整
local t = {foo0()} -- t = {}(一个空的table)
local t = {foo1()} -- t = {
}
local t = {foo2()} -- t = {
}
local t = {foo0(), foo2(), 4} -- t[1] = nil, t[2] =
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, t[3] = 4
function MyFunc() -- 返回a
return
foo1() -- 注:这里是
foo1(),而不是
(foo1())
end
--
foo1()和
(foo1())是两个完全不同的意思
print((foo0())) -- nil
print((foo1())) -- a
print((foo2())) -- a
|
变长参数
在C语言中,函数可以接受不同数量的实参,Lua中的函数也可以接受不同数量的实参,例如以下代码:
参数表中的3个点(…)表示该函数可接受不同数量的实参。当这个函数被调用时,它的所有参数都会被收集到一起。这部分收集起来的实参称为这个函数的“变长参数”。一个函数要访问它的变长参数时,仍需要用到3个点(…)。但不同的是,此时这3个点是作为一个表达式来使用的。在上例中,表达式{…}表示一个由所有变长参数构成的数组。在C语言中使用变长参数需要注意的问题,在Lua中同样需要注意。
通常一个函数在遍历其变长参数时只需要使用表达式{…},这就像访问一个table一样,访问所有的变长参数。然而在某些特殊的情况下,变长参数中可能会包含一些故意传入的nil,那么此时就需要用select来访问变长参数了。调用select时,必须传入一个固定实参selector和一系列变长参数。如果selector为数字n,那么select返回它的第n个可变实参;否则selector只能为字符串“#”,这样select会返回变长参数的总数,请看以下代码:
i = 1, select(
'#'
ottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-family: Consolas, ...)
do
local arg = select(i, ...) -- 得到第i个参数
-- Do something
else
select(‘#’,…)会返回所有变长参数的总数,其中包括nil(还记得table.maxn么?)对于Lua 5.0版本来说,变长参数则有另外一套机制。声明函数的语法是一样的,也是将3个点作为最后一个参数。但Lua 5.0没有提供“…”表达式。而是通过一个隐含的局部table变量“arg”来接受所有的变长参数。这个table还有一个名为“n”的字段,用来记录变长参数的总数,例如以下代码:
|