function declarations?

I have done java and C programming in the past and I guess im just not familiar with interpreted languages like lua. Does Lua have something like a function declaration or prototypes? for instance

local myListener = function( event )  
 print( "Listener called with event of type " .. event.name )  
end  
Runtime:addEventListener( "touch", myListener )   

this works but

Runtime:addEventListener( "touch", myListener )  
local myListener = function( event )  
 print( "Listener called with event of type " .. event.name )  
end  

this does not work… although it doesn’t even give me an error
however I dont understand why " Runtime:addEventListener( “touch”, myListener )" must come AFTER the mylistener declaration. I can see in the future this can be very annoying when you have a function calling another but it must come after it in the actual lua file.
[import]uid: 55057 topic_id: 10681 reply_id: 310681[/import]

It needs to come after the declaration as before the declaration happens that function is, naturally, undeclared.

Your second version is the same as saying:

  
Runtime:addEventListener( "touch", nil )  
  

You can use forward declarations like this though:

  
local myListener  
  
Runtime:addEventListener( "touch", myListener )   
  
myListener = function( event )  
 print( "Listener called with event of type " .. event.name )  
end  
  

Note, forward declarations work in regular situations however I haven’t tested them for listeners like this. [import]uid: 5833 topic_id: 10681 reply_id: 38779[/import]

@GrahamRanson: Great point with forward referencing, it is *extremely* useful in many cases.

In my own testing (not tested with adding event listeners), forward referencing—for whatever reason—hasn’t worked when it comes to setting listeners (particularly as arguments to functions, such as with ui.lua buttons and things like that).

It’s worth a shot when it comes to adding event listeners, but if it still doesn’t work, you might just have to place the function above the Runtime:addEventListener… line.

When I first started programming in Lua, that was my first major confusion spot—it seemed like everything was backwards. After a while, you get used to it and programming in Lua becomes second nature :slight_smile: [import]uid: 52430 topic_id: 10681 reply_id: 38783[/import]

Forward referencing is a useful tool and you should use it when it makes sense.
However, in the above example, there may be an additional problem which forward referencing doesn’t solve.

Assuming your code is in the global space, the problem is that your call to Runtime:addEventListener is being executed at start up. So that piece of code going from top to bottom is being executed before your listener is defined to addEventListener is still getting nil as a parameter. Forward referencing only works when you guarantee the values are defined before you actually use them.

Similarly, if your addEventListener was in some other callback that got invoked sometime after your listener variable was defined, this would work.

[import]uid: 7563 topic_id: 10681 reply_id: 38817[/import]

Thanks alot guys, the forward declarations work great for the event listeners. [import]uid: 55057 topic_id: 10681 reply_id: 38889[/import]