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

Question:
In Lua what is a forward declared function?

In Lua a function is a chunk of code pointed to by a variable.

If I use that variable before I set it to the code-chunk, I get an error.

Lua allows me to forward declare the variable before I set it.

Sometimes you will see a forward declared function in other's code. The author of that code may have wanted to deal with this situation.

Here is a demo of what forward declared functions look like:
-- ~/ks/b/lua/forward_declare.lua

-- Sometimes I will see forward declared functions.

local fnc1, fnc2 -- These now forwarded

function fnc1()
  return 1
end

function fnc2()
  return fnc1()
end

print(fnc2())


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