lua实践-函数

1. 函数声明的顺序

* 函数a和b均为local function,b调用a函数,这种情况a要在b之前定义,否则会报错

local function a()
    print("in a function")
end

local function b()
    a()
end

b()

输出

in a function
[Finished in 0.0s]

 

b在a之前定义就会报错

local function b()
    a()
end

local function a()
    print("in a function")
end

b()

输出

lua: E:\testLua\localfun.lua:13: attempt to call global 'a' (a nil value)
stack traceback:
    E:\testLua\localfun.lua:13: in function 'b'
    E:\testLua\localfun.lua:20: in main chunk
    [C]: ?

 

* 如果a为global function,a和b的声明顺序就无关紧要

local function b()
    a()
end

function a()
    print("in a function")
end
b()

输出

in a function
[Finished in 0.0s]

 

posted @ 2013-06-07 15:19  *tingliang*  阅读(209)  评论(0编辑  收藏  举报