Order of Function Declarations

I am making excellent progress in learning/developing in LUA thanks to all of the examples here…awesome… but suddenly I got stuck on what I think is probably a simple solution, but an hour later, I am still stuck.

Here is a simplified idea of my problem:

local function A()
– calls function b
B()
end
local function B()
–calls function a
A()
end

my problem is that above doesn’t work… in function A(), I can’t call function B() unless I define function B ABOVE or PRIOR to it being called, but then if I do that and declare B above A, then B can’t call A …circular…

I have never encountered this in “higher” languages, I’m sure there must be some work-around Lua … any ideas?? Help !

THanks,

[import]uid: 92927 topic_id: 15898 reply_id: 315898[/import]

There are many solutions to this, the simplest for now is just to use forward declarations.

[lua]local a = nil
local b = nil

a = function()
print( “In A” )
end

b = function()
print( “In B” )
a()
end

b()[/lua]

Hope that helps. [import]uid: 5833 topic_id: 15898 reply_id: 58801[/import]

as simple as this :slight_smile:

[lua]local A,B

function A()
– calls function b
B()
end

function B()
–calls function a
A()
end[/lua] [import]uid: 71210 topic_id: 15898 reply_id: 58810[/import]

Renjith, that can get into a deadlock situation as A -> B and B-> A, which will again spawn B and so on.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15898 reply_id: 58825[/import]

SING: never ending story … maybe til the stack explodes

but the problem itself should be resolved. [import]uid: 70114 topic_id: 15898 reply_id: 58841[/import]