lua function
local function demoA() print "demoA" end local function demoB () print "demoB" end local function forfun() print "in forfun" end forfun( demoB ) print("========") forfun( demoA() )
输出结果:
in forfun ======== demoA in forfun [Finished in 0.0s]
多增加一种情况:
local function forfun_demo( pf ) pf() print "in forfun_demo" end forfun_demo( demoB )
输出结果为:
demoB in forfun_demo
在此,偷偷感谢小伙伴M.S.Y的分享。
附带添加fun3()的c语言版本对比:
1 #include <stdio.h> 2 3 void fun1(){ 4 printf("\n Hello World! \n"); 5 } 6 7 // void fun2(){ 8 // fun1(); 9 // } 10 11 void fun3(void(*p)()){ 12 p(); 13 } 14 15 int main(){ 16 17 // fun2(fun1); 18 fun3(fun1); 19 20 return 0; 21 }