Lua chapter 4
来源:互联网 发布:中南大学网络教育网 编辑:程序博客网 时间:2024/05/29 03:25
1. 函数可以返回多个值 return a, b, c; 但是如果函数不是作为表达式的最后一个元素的话,仅返回第一个
如:
function f2() return "a", "b" end;
x, y = f2() -> x = "a", y = "b";
x, y = f2(), 1 -> x = "a", y = nil;
2. 可以将一个函数调用放入一对圆括号中,从而迫使它只返回一个结果
如:
print(f2()); --> a ,b
print( (f2()) ); --> a
3. unpack() 函数,用于解压table
a = {1,2,3};
print(a); --> table 地址
4. 变长参数,参数表中的3个点(...)表示该函数可接受的不同数量的实参。
function add(...)
print(...);
-- 遍历变长参数
for i=1, select("#", ...) do -- select("#", ...) 获取长度
local arg = select(i, ...);
print(arg);
end;
local s = 0;
for i, v in ipairs{...} do
s = s + v;
end;
return s;
end;
如:
function f2() return "a", "b" end;
x, y = f2() -> x = "a", y = "b";
x, y = f2(), 1 -> x = "a", y = nil;
2. 可以将一个函数调用放入一对圆括号中,从而迫使它只返回一个结果
如:
print(f2()); --> a ,b
print( (f2()) ); --> a
3. unpack() 函数,用于解压table
a = {1,2,3};
print(a); --> table 地址
print( unpack(a) ); --> 1 2 3
4. 变长参数,参数表中的3个点(...)表示该函数可接受的不同数量的实参。
function add(...)
print(...);
-- 遍历变长参数
for i=1, select("#", ...) do -- select("#", ...) 获取长度
local arg = select(i, ...);
print(arg);
end;
local s = 0;
for i, v in ipairs{...} do
s = s + v;
end;
return s;
end;
print(add(1,2,3,4,5));
3 0
- Lua chapter 4
- Lua chapter 1
- Lua chapter 2
- Lua chapter 3
- Lua chapter 5
- Lua chapter 6
- Lua chapter 7
- Lua chapter 8 协同程序
- chapter 27 Calling C from Lua
- chapter 4
- Chapter 4
- chapter 4
- Chapter 4
- Chapter 4
- Chapter 4
- Chapter 4
- Chapter 4
- Chapter 4
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
- java 线程关闭小结
- DLL(动态链接库)专题
- win7&Cocos2dx2.2&Android最简单方案
- [x86/x64]基础
- Lua chapter 4
- VmWare虚拟机安装Ubuntu12.04.1
- 安卓下cocos2dx实现cpp部分在线更新
- window 下 mysql 主从复制
- 浅谈arduino的bootloader
- K-mean clustering 算法
- Linux 档案与目录管理
- 多线程JDK1.5版本以上新方法
- linux嵌入式系统下实现U盘、SD卡自动挂载功能