【Lua】Lua学习五:函数([Lua] Lua learning 5: function)

一:

二:多重返回值

多重赋值:如果一个函数调用不是一系列表达式的最后一个元素,那么将只产生一个值

函数调用时传入的实参列表

table的构造式

return语句

三:变长参数

function add(…)

return

end

四:具名实参

五:函数

Lua中的函数都是匿名函数

function foo(x) return 2*x end 等价于 foo = function (x) return 2*x end

table.sort(network, function (a,b) return (a.name > b.name) end)

可以接受函数作为参数的函数,称为高阶函数

六:闭包

Lua中只有closure,没有函数。因为函数本身就是一种特殊的closure。

 沙盒?

七:非全局函数

八:正确的尾调用

应用:编写状态机?

————————

I:

2: Multiple return values

Multiple assignment: if a function call is not the last element of a series of expressions, only one value will be generated

List of arguments passed in during function call

Construction of table

return statement

3: Variable length parameter

function add(…)

return

end

4: Named argument

5: Function

All functions in Lua are anonymous

function foo(x) return 2*x end 等价于 foo = function (x) return 2*x end

table.sort(network, function (a,b) return (a.name > b.name) end)

Functions that can accept functions as parameters are called higher-order functions

6: Closure

Lua has only closures and no functions. Because the function itself is a special closure.

Sandbox?

7: Non global function

8: Correct tail call

Application: write state machine?