---函数多个返回值functiontest01(arr)local maxIndex =1local maxNum = arr[maxIndex]for i, v inpairs(arr)doif maxNum<v then
maxIndex = i
maxNum = v
endendreturn maxIndex,maxNum
end
arr ={4,6,8,2,13,54,1,32}print(test01(arr))---可变参数functiontest02(...)local res =0;local t ={...}for i, v inpairs(t)do
res = res + v
endreturn res/#t
endprint(test02(1,2,3,4))---函数作为参数functionmyPrint(val)print("自定义打印输出函数:"..val)endfunctiontest03(i,j,func)func(i+j)endtest03(1,2,myPrint)---数学库相关print("===================================================")print(math.abs(-20))print(math.max(1,1,2,3,4,5))print(math.min(1,4,2,1,3,5,7))print(math.sqrt(25))print(math.floor(14.932))print(os.date())print(os.time())--真随机数functionGetRandomNumber(min,max)return math.random(min,max)end
math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,9)))for i =1,10doprint(GetRandomNumber(1,1000))end---闭包函数functiontest04(i)print(i)returnfunction()return i +1endend
v =test04(2)print(v())functiontest05(i)return i
endprint(test05(99))functionResMul()local num =10functionfun1()print(num)endfunctionfun2()print(num+99)endreturn fun1,fun2
end
res1,res2 =ResMul()res1()res2()functiontest06()return(string.find("zzs is zzs","is",1))endprint(test06())