--return the index of max number and himself
--函式可以返回多個值
function
get_max( t )
local index = 1
local max = t[1
]
for i, v in
ipairs( t ) do
if v > max then
max =v
index =i
endend
return
index, max
end--
如果函式的引數為表或者字串 可以省略小括號
--index, value = get_max
--print( index, value )
--返回乙個子字串的開始位置和結束位置
function
get_pos( str, substr )
s, e = string.find
( str, substr )
return
s, e
ends, e = get_pos( "
hello,ghostwu,how are you?
", "
ghostwu")
print( '
字串的開始位置
' .. s .. '
, 結束位置為:
' .. e )
function foo() endfunction foo1() return'a
'end
function foo2() return'a
', 'b'
end--
按位置接收, 多餘的被丟棄
x, y =foo2()
print( x, y ) --
a, b
x, y =foo1()
print( x, y ) --
a, nil
x, y =foo()
print( x, y ) --
nil, nil
--函式呼叫表示式不是最後乙個表示式, 只返回乙個值
x, y = foo2(), 10
print( x, y ) --
a, 10
print( foo2(), 100 ) --
a 100
t = --
a, 20
for i, v in
ipairs( t ) do
io.write( v, "
\t" ) --
a, 20
endio.write( "
\n" )
> unpack( )
> print( unpack )
1020
30> a, b = unpack
( a, b )
1020
> s, e = string.find( "
hello,ghostwu
", "
ghost
( s, e )711
> s, e = string.find( unpack ) ; print
( s, e )711
函式多個返回值與unpack的用法
return the index of max number and himself 函式可以返回多個值 function get max t local index 1 local max t 1 for i,v in ipairs t do if v max then max v index i...
python函式多個返回值
python函式可以返回多個值嗎?答案是肯定的。比如在遊戲中經常需要從乙個點移動到另乙個點,給出座標 位移和角度,就可以計算出新的新的座標 import math def move x,y,step,angle 0 nx x step math.cos angle ny y step math.si...
Lua函式的多個返回值
lua中的函式的乙個很特殊也很有用的性質,即可以有多個返回值。包括一些內建的函式就是這樣。比如string.find函式,在給定的字串中查詢乙個pattern,如果有匹配的部分,則返回對應的頭 尾的兩個索引值 如果不存在匹配,則返回nil。當然,使用者定義的函式也可以有多個返回值,通過return關...