openresty开发系列14--lua基础语法3函数
openresty開發(fā)系列14--lua基礎(chǔ)語法3函數(shù)
一)function (函數(shù))
有名函數(shù):
optional_function_scope function function_name( argument1, argument2, argument3..., argumentn)
??? function_body
??? return result_params_comma_separated
end
optional_function_scope: 該參數(shù)是可選的制定函數(shù)是全局函數(shù)還是局部函數(shù),未設(shè)置該參數(shù)默認為全局函數(shù),如果你需要設(shè)置函數(shù)為局部函數(shù)需要使用關(guān)鍵字 local。
function 函數(shù)定義關(guān)鍵字
function_name: 指定函數(shù)名稱。
argument1, argument2, argument3..., argumentn: 函數(shù)參數(shù),多個參數(shù)以逗號隔開,函數(shù)也可以不帶參數(shù)。
function_body: 函數(shù)體,函數(shù)中需要執(zhí)行的代碼語句塊。
result_params_comma_separated: 函數(shù)返回值,Lua語言函數(shù)可以返回多個值,每個值以逗號隔開
end:函數(shù)定義結(jié)束關(guān)鍵字
1)函數(shù)實例
--[[ 函數(shù)返回兩個值的最大值 --]]
function max(num1, num2)
?? if (num1 > num2) then
????? result = num1;
?? else
????? result = num2;
?? end
?? return result;
end
-- 調(diào)用函數(shù)
print("兩值比較最大值為 ",max(10,4))
print("兩值比較最大值為 ",max(5,6))
匿名函數(shù)
optional_function_scope function_name = function (argument1, argument2, argument3..., argumentn)
?? ?function_body
?? ?return result_params_comma_separated
end
有名函數(shù)的定義本質(zhì)上是匿名函數(shù)對變量的賦值。為說明這一點,考慮
function foo()
end
等價于
foo = function ()
end
類似地,
local function foo()
end
等價于
local foo = function ()
end
local function 與 function 區(qū)別
1 使用function聲明的函數(shù)為全局函數(shù),在被引用時可以不會因為聲明的順序而找不到
2 使用local function聲明的函數(shù)為局部函數(shù),在引用的時候必須要在聲明的函數(shù)后面
function test()
??? test2()
??? test1()
end
local function test1()
??? print("hello test1")
end
function test2()
??? print("hello test2")
end
test()
-----------------
local function test1()
??? print("hello test1")
end
function test()
??? test2()
??? test1()
end
function test2()
??? print("hello test2")
end
test()
==========================
函數(shù)參數(shù)
1) 將函數(shù)作為參數(shù)傳遞給函數(shù)
local myprint = function(param)
?? print("這是打印函數(shù) -?? ##",param,"##")
end
local function add(num1,num2,functionPrint)
?? result = num1 + num2
?? functionPrint(result)
end
add(2,5,myprint)
2)傳參數(shù),lua參數(shù)可變
local function foo(a,b,c,d)
?? ?print(a,b,c,d)
end
a、若參數(shù)個數(shù)大于形參個數(shù),從左向右,多余的實參被忽略
b、若實參個數(shù)小于形參個數(shù),從左向右,沒有被初始化的形參被初始化為nil
c、Lua還支持變長參數(shù)。用...表示。此時訪問參數(shù)也要用...,如:
function average(...)
?? result = 0
?? local arg={...}
?? for i,v in ipairs(arg) do
????? result = result + v
?? end
?? print("總共傳入 " .. #arg .. " 個數(shù)")
?? return result/#arg
end
print("平均值為",average(1,2,3,4,5,6))
3)返回值
Lua函數(shù)允許返回多個值,返回多個值時,中間用逗號隔開
函數(shù)返回值的規(guī)則:
1)若返回值個數(shù)大于接收變量的個數(shù),多余的返回值會被忽略掉;
2)若返回值個數(shù)小于參數(shù)個數(shù),從左向右,沒有被返回值初始化的變量會被初始化為nil
?? ?local function init()
?? ??? ?return 1,"lua";
?? ?end
?? ?local x,y,z = init();
?? ?print(x,y,z);
注意:
1)當一個函數(shù)返回一個以上的返回值,且函數(shù)調(diào)用不是一個列表表達式的最后一個元素,那么函數(shù)只返回第一個返回值
?? ?local function init()
?? ??? ?return 1,"lua";
?? ?end
?? ?local x,y,z = 2,init();? --local x,y,z = init(),2;
?? ?print(x,y,z);
?? ?
??? 2)如果你確保只取函數(shù)返回值的第一個值,可以使用括號運算符
??? local function init()
?? ??? ?return 1,"lua";
?? ?end
?? ?local x,y,z = 2,(init());
?? ?print(x,y,z);
轉(zhuǎn)載于:https://www.cnblogs.com/reblue520/p/11429715.html
總結(jié)
以上是生活随笔為你收集整理的openresty开发系列14--lua基础语法3函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列13--lua
- 下一篇: openresty开发系列15--lua