当前位置:网站首页>lua入门案例实战1234定义函数与标准函数库功能
lua入门案例实战1234定义函数与标准函数库功能
2022-07-31 22:50:00 【海宝7号】
– Example – Functions.
函数相关的案例自定义
定义函数1-简单调用
– Define a function without parameters or return value.
案例如下
function myFirstLuaFunction()
print("My first lua function was called")
end
– Call myFirstLuaFunction.
myFirstLuaFunction()
-------- Output ------
My first lua function was called
定义函数2-带返回值
– Example – More functions.
– Define a function with a return value.
function mySecondLuaFunction()
return "string from my second function"
end
– Call function returning a value.
a=mySecondLuaFunction("string")
print(a)
-------- Output ------
string from my second function
定义函数3-多参数返回
– Define function with multiple parameters and multiple return values.
function myFirstLuaFunctionWithMultipleReturnValues(a,b,c)
return a,b,c,"My first lua function with multiple return values", 1, true
end
a,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValues(1,2,"three")
print(a,b,c,d,e,f)
-------- Output ------
1 2 three My first lua function with multiple return values 1 true
定义函数4-全局变量与局部变量
– All variables are global in scope by default.
b=“global”
– To make local variables you must put the keyword ‘local’ in front.
function myfunc()
local b=" local variable"
a="global variable"
print(a,b)
end
调用函数
myfunc()
print(a,b)
-------- Output ------
global variable local variable
global variable global
定义函数5-格式化输出
function printf(fmt, ...)
io.write(string.format(fmt, ...))
end
printf("Hello %s from %s on %s\n",
os.getenv"USER" or "there", _VERSION, os.date())
-------- Output ------
Hello there from Lua 5.1 on 07/30/22 14:34:25
定义注释多行
-- Example 31 --[[
Standard Libraries
Lua has standard built-in libraries for common operations in
math, string, table, input/output & operating system facilities.
External Libraries
Numerous other libraries have been created: sockets, XML, profiling,
logging, unittests, GUI toolkits, web frameworks, and many more.
]]
-------- Output ------
标准函数库
标准数学库
– Math functions:
– math.abs, math.acos, math.asin, math.atan, math.atan2,
– math.ceil, math.cos, math.cosh, math.deg, math.exp, math.floor,
– math.fmod, math.frexp, math.huge, math.ldexp, math.log, math.log10,
– math.max, math.min, math.modf, math.pi, math.pow, math.rad,
– math.random, math.randomseed, math.sin, math.sinh, math.sqrt,
– math.tan, math.tanh
print(math.sqrt(9), math.pi)
-------- Output ------
3 3.1415926535898
标准库string
– String functions:
– string.byte, string.char, string.dump, string.find, string.format,
– string.gfind, string.gsub, string.len, string.lower, string.match,
– string.rep, string.reverse, string.sub, string.upper
print(string.upper("lower"),string.rep("a",5),string.find("abcde", "cd"))
-------- Output ------
LOWER aaaaa 3 4
标准库table
– Table functions:
– table.concat, table.insert, table.maxn, table.remove, table.sort
a={2}
table.insert(a,3);
table.insert(a,4);
table.sort(a,function(v1,v2) return v1 > v2 end)
for i,v in ipairs(a) do print(i,v) end
-------- Output ------
1 4
2 3
3 2
标准输入输出IO
– IO functions:
– io.close , io.flush, io.input, io.lines, io.open, io.output, io.popen,
– io.read, io.stderr, io.stdin, io.stdout, io.tmpfile, io.type, io.write,
– file:close, file:flush, file:lines ,file:read,
– file:seek, file:setvbuf, file:write
print(io.open("file doesn't exist", "r"))
-------- Output ------
nil file doesn’t exist: No such file or directory 2
系统函数配置OS库
– OS functions:
– os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv,
– os.remove, os.rename, os.setlocale, os.time, os.tmpname
print(os.date())
-------- Output ------
07/30/22 14:39:54
扩展库支持
– Lua has support for external modules using the ‘require’ function
– INFO: A dialog will popup but it could get hidden behind the console.
require( "iuplua" )
ml = iup.multiline
{
expand="YES",
value="Quit this multiline edit app to continue Tutorial!",
border="YES"
}
dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER",}
dlg:show()
print("Exit GUI app to continue!")
iup.MainLoop()
-------- Output ------
failed to load & run sample code
暂未配置结束。
```css
-- Example 38 --[[
To learn more about Lua scripting see
Lua Tutorials: http://lua-users.org/wiki/TutorialDirectory
"Programming in Lua" Book: http://www.inf.puc-rio.br/~roberto/pil2/
Lua 5.1 Reference Manual:
Start/Programs/Lua/Documentation/Lua 5.1 Reference Manual
Examples: Start/Programs/Lua/Examples
]]
-------- Output ------
Press 'Enter' key for next example
边栏推荐
- 程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
- 基于单片机GSM的防火防盗系统的设计
- Input and output optimization
- 嵌入式开发没有激情了,正常吗?
- 网络安全--通过握手包破解WiFi(详细教程)
- SQL27 View user details of different age groups
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
- SQL注入 Less38(堆叠注入)
- 景区手绘地图的绘制流程
- 一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
猜你喜欢
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
21. Support Vector Machine - Introduction to Kernel Functions
【ACM】2022.7.31训练赛
Bika LIMS open source LIMS set - use of SENAITE (detection process)
A high-quality WordPress download site template theme developed abroad
Chapter Six
Audio alignment using cross-correlation
ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
数据分析(一)——matplotlib
随机推荐
cas and spin locks (is lightweight locks spin locks)
Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
Flink_CDC construction and simple use
「APIO2010」巡逻 题解
[QNX Hypervisor 2.2 User Manual]9.16 system
[QNX Hypervisor 2.2 User Manual]9.14 set
HTC using official firmware as bottom bag made ROM brush card bag tutorial
登录业务实现(单点登录+微信扫码+短信服务)
面试突击69:TCP 可靠吗?为什么?
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Drawing process of hand-drawn map of scenic spots
"SDOI2016" Journey Problem Solution
[QNX Hypervisor 2.2用户手册]9.16 system
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
How to reduce the gap between software design and implementation
"SDOI2016" Journey Problem Solution
手写一个简单的web服务器(B/S架构)
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
Go mode tidy reports an error go warning “all” matched no packages