syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Lua how to pass function to function?

In Lua I can pass function1 to function2 as an argument.

I found that function1 needs to be an anonymous function:
-- f1f2.lua

function f1()
  return 'world'
end

function f2(f)
  str = f()
  return 'hello '..str
end
-- This fails:
-- print(f2(f1()))

f3 = function() return 'world' end
-- This fails:
-- print(f2(f3()))

-- This works:
print(f2(function() return 'world' end))


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me