returning objects - is it necessary?

I read somewhere that I should return objects at the end of functions, and return true after listeners. So I have implemented these statements but what to they actually do? Are they necessary?  

thanks.

----------------------------------- FUNCTION:  myListener ----------------------------------- myListener = function ( event )      if ( event.phase == "began" ) then         ....         elseif ( event.phase == "ended" ) then     ...         end return true end ----------------------------------- -- 12 FUNCTION: displayMainMenu ----------------------------------- displayMainMenu = function() menuGroup = display.newGroup() local mytextObject = display.newText(menuGroup, "some text here" , 100, 100, FONT\_NAME, MENU\_FONT\_SIZE) ... [add a bunch more objects to menuGroup] return menuGroup    end 

If you don’t return true after listeners, it ‘bubbles up’ to the next object (for things like ‘tap’), so if you check for a tap on a circle, and return false, it will ‘offer’ it to (say) a square behind it.

It certainly isn’t a requirement to return objects from a function. It is conceptually better to write

displayMainMenu = function() local menuGroup = display.newGroup() -- do stuff to it return menuGroup

myMenu = displayMainMenu()

than:

displayMainMenu = function() local menuGroup = display.newGroup() -- do stuff to it myMenu = menuGroup end

Even though they do pretty much the same thing (probably !) the first one is encapsulated - it creates and returns a menuGroup object, and doesn’t affect anything else outside it.

is the 

myMenu = displayMainMenu()

best practice? I call displayMainMenu() but I never assign that call to a variable.

thanks,

That’s fine - it just looked like menuGroup in your example was a global.

I just wrote it that way in the example for simplicity. My ‘globals’ aren’t true globals at all, but live in a table called myData (as per this tutorial http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/), so it’s actually myData.menuGroup. I didn’t make it local because I need to remove the group from inside a listener function.

thanks for you help!

If you don’t return true after listeners, it ‘bubbles up’ to the next object (for things like ‘tap’), so if you check for a tap on a circle, and return false, it will ‘offer’ it to (say) a square behind it.

It certainly isn’t a requirement to return objects from a function. It is conceptually better to write

displayMainMenu = function() local menuGroup = display.newGroup() -- do stuff to it return menuGroup

myMenu = displayMainMenu()

than:

displayMainMenu = function() local menuGroup = display.newGroup() -- do stuff to it myMenu = menuGroup end

Even though they do pretty much the same thing (probably !) the first one is encapsulated - it creates and returns a menuGroup object, and doesn’t affect anything else outside it.

is the 

myMenu = displayMainMenu()

best practice? I call displayMainMenu() but I never assign that call to a variable.

thanks,

That’s fine - it just looked like menuGroup in your example was a global.

I just wrote it that way in the example for simplicity. My ‘globals’ aren’t true globals at all, but live in a table called myData (as per this tutorial http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/), so it’s actually myData.menuGroup. I didn’t make it local because I need to remove the group from inside a listener function.

thanks for you help!