当前位置:网站首页>Lua函数之非全局函数
Lua函数之非全局函数
2022-07-26 23:51:00 【森明帮大于黑虎帮】
Lua非全局函数
注意:在Lua中调用一个函数要在调用之前定义,否则出错。
local fact = function(n)
if (n == 1) then
return n;
else
return n + fact(n-1)
end
end
print("result:", fact(2))
错误原因:
c:/Users/Administrator/.vscode/extensions/actboy168.lua-debug-1.59.0/runtime/win32-x64/lua54/lua.exe: d:\vscode lua����\hello world\Untitled-1.lua:1807: attempt to call a nil value (global 'fact')
stack traceback:
d:\vscode lua����\hello world\Untitled-1.lua:1807: in local 'fact'
d:\vscode lua����\hello world\Untitled-1.lua:1810: in main chunk
[C]: in ?
自己很纳闷,为什么会出现问题?看到报错,都会想到是没有识别,作用域的问题,结果参考[1],“上面这种方式导致Lua编译时遇到 fact(n-1) 并不知道他是局部函数 fact,Lua会去查找是否有这样的全局函数 fact”,会有一个大概的了解。这里也给了一个解决办法:
local fLocal
fLocal = function(n)
if (n == 1) then
return n;
else
return n + fLocal(n-1)
end
end
print("result:", fLocal(2))

分析一下,函数fact中是不能识别自己的,那么编译器在到这里的时候还不能认识fact。这让我想起来某个语言的定义(具体是什么忘记了):只有匹配到"}",才算定义完毕,编译器才会识别这个定义。那是什么意思呢?拿这里的local fact来说只有编译器解释到“end”的时候才表示fact定义完毕,才会有这个变量,不然都是非法的,所以解决办法就是把fact单独拿出来,这样编译器扫描一遍的时候就认识了,不会报错。
Lua初识之非全局函数
-----表和函数放在一起
lib = {
}
lib.foo = function(x, y)
return x + y
end
lib.goo = function( x, y)
return x - y
end
print(lib.foo(3, 5)) --> 8
print(lib.goo(3, 5)) --> -2
--------------------------
--------使用表构造函数
lib = {
foo = function(x, y) return x + y end,
goo = function(x, y) return x - y end
}
print(lib.foo( 3, 5)) --> 8
print(lib.goo( 3, 5)) --> -2
---------------------------
lib ={
}
function lib.foo(x, y)
return x + y
end
function lib.goo(x, y)
return x - y
end
print(lib.foo(3, 5)) --> 8
print(lib.goo(3, 5)) --> -2
----------------------------
do
local f = function(x)
return x
end
g = function(n)
return n * n
end
local gg = function(m)
return f(m * m)
end
print(f(5)) --> 5
print(g(6)) --> 36
print(gg(8)) --> 64
end
print(g(9)) --> 81
--print(f(5)) --> 在模块外调用局部函数 f()报错
------------------------------------
do
local function f(n)
print(n * n)
end
f(10) --> 100
end
--f(10) --> 局部函数在外部使用报错
---------------------------------
do
local fact
fact = function(n)
if n == 0 then
return 1
else
return n * fact(n - 1)
--> 此处用到了 fact() ,要提前声明 局部函数local fact,
--> 不提前声明的话,会直接在全局函数中调用
end
end
print(fact(5)) --> 120
end
-------------------------
-----尾调函数
do
function f(x)
return g(x) --> 此种用法为尾调函数,即返回值调用 其他函数
end
function ff(x)
g(x)
return --> 此种不属于尾调函数,
end
function g(n)
return n
end
print(f(5))
end
边栏推荐
- Jmeter接口测试, 快速完成一个单接口请求
- 平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
- Blog competition dare to try BAC for beginners
- Cookie addition, deletion, modification and query methods
- 贪心——376. 摆动序列
- How small programs help the new model of smart home ecology
- FormData的使用
- 中断、信号、系统调用
- Summary of dataset operations in ppocrlabel format.
- Hcip first day
猜你喜欢
随机推荐
面试突击68:为什么 TCP 需要 3 次握手?
I was fired at the age of 30. I want to understand a few things
30岁被裁,我想明白的几件事....
【Code】剑指offer 04二维数组中的查找
Jmeter接口测试, 快速完成一个单接口请求
Static routing experiment configuration
听说你们面试都跪在了接口测试上?
关于序列化变量保存的坑,加了索引器的数据不能序列化
How to do the system security test? Let's talk about it in detail
函数栈帧详解
Sort icons with swiper
Smooth data migration from single table to sub table
[Li Kou] 1859. Sort sentences
JMeter interface test, quickly complete a single interface request
LeetCode->二分法(三)
动态设置小程序swiper的高度
[brother Yang takes you to play with the linear table (III) - two way linked list]
Redis distributed lock implemented by annotation
Mysql 5.7 取分组第一条
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round







![[redis] five common data types](/img/0f/17945b6f3a7735d9a569296c2c0e2a.png)

