Functions in event listeners...

So here’s the deal.  I am trying to run a function inside of an event listener as such:

    local addBtnStart = function ()         btnStartGame = display.newText( "Start Game", 0, 0, "Helvetica", 60 )         btnStartGame.x = display.contentCenterX         btnStartGame.y = display.contentCenterY-200         btnStartGame.alpha=0         transition.to( btnStartGame, {time=1000, alpha=1, y=display.contentCenterY, x=display.contentCenterX } )         btnStartGame:addEventListener ( "tap", woof() )     end  

Well, woof() causes errors.  As a global function waaay up top I have…

local function woof() audio.play( sndWoof ) end

HOWEVER, if I change the event listener to…

 btnStartGame:addEventListener ( "tap", woof )

AND change the function woof into a function inside a variable as such…

local woof = function() audio.play( sndWoof ) end

It works!  What have I stumbled upon?  What is the difference between a function and a function inside a variable?  Why will a function inside a variable work in an event listener, but an actual function will NOT work in such an instance?  The error I get when trying to run a function is attached as an image.  

I’m new to the concept of a function inside of a variable, but it seems I can add say…

local woof = function( var1 )

or multiple in-values.  What’s the difference between a function inside of a variable and an actual function?  Why are they treated differently in my code (why does one work and the other doesn’t)?

Thanks!

-Chris

woof is the function, whereever you write woof, there will be an “uncalled” function - that is - a kind of pointer to the function

woof() calls the functions, wherever you write woof(), there will only be the return value(s) of the function, nothing else

so

btnStartGame:addEventListener ( “tap”, woof )       is correct

btnStartGame:addEventListener ( “tap”, woof() )     is wrong  (as long as woof doesn’t return yet another function)

local function woof()

is just another way to write

local woof = function()

see here http://www.lua.org/pil/6.html where it says “syntactic sugar”

I figured they were the same.  I also found out that yea, woof calls the function woof.  I didn’t realize this at the time lol.  The main problem I had in addition to this was say passing variables through the function, so…

btnStartGame:addEventListener ( “tap”, woof( var1, var2 )  

Only to realize that you end up doing something like this…

(another piece of my code)

                        label = "Back to menu",                             onEvent = function( obj )                                 buttonHit( obj, "page\_menu", "slideRight")                             end

So much to learn, I’m having fun with it (: Thanks for the help!

-Chris

woof is the function, whereever you write woof, there will be an “uncalled” function - that is - a kind of pointer to the function

woof() calls the functions, wherever you write woof(), there will only be the return value(s) of the function, nothing else

so

btnStartGame:addEventListener ( “tap”, woof )       is correct

btnStartGame:addEventListener ( “tap”, woof() )     is wrong  (as long as woof doesn’t return yet another function)

local function woof()

is just another way to write

local woof = function()

see here http://www.lua.org/pil/6.html where it says “syntactic sugar”

I figured they were the same.  I also found out that yea, woof calls the function woof.  I didn’t realize this at the time lol.  The main problem I had in addition to this was say passing variables through the function, so…

btnStartGame:addEventListener ( “tap”, woof( var1, var2 )  

Only to realize that you end up doing something like this…

(another piece of my code)

                        label = "Back to menu",                             onEvent = function( obj )                                 buttonHit( obj, "page\_menu", "slideRight")                             end

So much to learn, I’m having fun with it (: Thanks for the help!

-Chris