Lua - 2 函数

1。可变数目的参数

-- 可变数目的参数,参数自动存储在名为arg的table

function funky_print (...)

for i=1, arg.n do

print("FuNkY: " .. arg[i])

end

end

funky_print("one", "two", "three")

输出结果:

FuNkY: one

FuNkY: two

FuNkY: three

2。

-- 以table作为参数,这里很奇妙

--print_contents{x=10, y=20}这句参数没加圆括号, 因为以单个table为参数的时候, 不需要加圆括号

--for k,v in t do 这个语句是对table中的所有值遍历, k中存放名称, v中存放值

function print_contents(t)

for key,value in t do

print(key .. " is " .. value)

end

end

print_contents{x=10, y=20}

输出结果:

y is 20

x is 10

3。