Function inside another Function

Hello guys, I wanted to ask if it was right to create a function inside another function.

I created a function main and inside other function, correct? The menu is OK, I just wanted to know if it was correct to do so or not.

There are plenty of valid reasons to have functions inside of functions.  Generally its because the inside function is only useful inside the function and you’ve declared it “local” so that it is indeed only available there.   Having a function that is more widely usable inside another function would be a bad practice.

function main()

    --bg e tap

    bg=display.newImage(‘media/bg.jpg’, true)

    tap=display.newImage(‘media/tap.png’)

    tap.x=240

    tap.y=420

    

    --start

    start=display.newImage(‘media/start.png’)

    start.x=400

    start.y=160    

    --credits

    credits=display.newImage(‘media/credits.png’)

    credits.x=380

    credits.y=190

    

    

    function start:touch(event)

        if event.phase== “began” then

            print (“ToccoStart”)

            --livello1()                        

        end

    end

    

    

    function credits:touch(event)

        if event.phase== “began” then

            print (“ToccoCredits”)

            transition.to(bg,{time=1000, y=0})    

            transition.to(start,{time=1000, y=-160})

            transition.to(credits,{time=1000, y=-130})

            transition.to(tap,{time=2500, y=280})        

        end

    end

end

I did so, but with many other functions, not good?

In this case, thats probably okay because your providing a pointer to the function for your objects.  They are not really global, nor are they really scoped to only live in main.lua.

Ok, now i create a Storyboard. ALL OK! Thanks

There are plenty of valid reasons to have functions inside of functions.  Generally its because the inside function is only useful inside the function and you’ve declared it “local” so that it is indeed only available there.   Having a function that is more widely usable inside another function would be a bad practice.

function main()

    --bg e tap

    bg=display.newImage(‘media/bg.jpg’, true)

    tap=display.newImage(‘media/tap.png’)

    tap.x=240

    tap.y=420

    

    --start

    start=display.newImage(‘media/start.png’)

    start.x=400

    start.y=160    

    --credits

    credits=display.newImage(‘media/credits.png’)

    credits.x=380

    credits.y=190

    

    

    function start:touch(event)

        if event.phase== “began” then

            print (“ToccoStart”)

            --livello1()                        

        end

    end

    

    

    function credits:touch(event)

        if event.phase== “began” then

            print (“ToccoCredits”)

            transition.to(bg,{time=1000, y=0})    

            transition.to(start,{time=1000, y=-160})

            transition.to(credits,{time=1000, y=-130})

            transition.to(tap,{time=2500, y=280})        

        end

    end

end

I did so, but with many other functions, not good?

In this case, thats probably okay because your providing a pointer to the function for your objects.  They are not really global, nor are they really scoped to only live in main.lua.

Ok, now i create a Storyboard. ALL OK! Thanks