通过lua自带例子学习lua 06 (26-30)

-- Example 26 -- Functions.

-- 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

-- Example 27 -- 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

-- Example 28 -- More functions.

-- 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

-- Example 29 -- Variable scoping and functions.

-- 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

-- Example 30 -- Formatted printing.

-- An implementation of printf.

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 04/06/13 16:16:02

 

转载于:https://www.cnblogs.com/sdlypyzq/archive/2013/04/06/3002555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值