Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Does return break the function?

AnatolyAnatoly Mod
in Questions Posts: 842

Soda touch events return true if the touch registers. So in your touched function you can put something like

if Soda.touched(touch) then return end --exit if Soda touch detected

I should update the Main template to reflect this. Thanks for raising this.

So please help me!

  • trying this times by myself, but nothing happens :( *

Comments

  • dave1707dave1707 Mod
    Posts: 7,753

    return will cause an exit from a function.

  • AnatolyAnatoly Mod
    Posts: 842

    And if you call from touched(t) a function, in that called function will return only break that function or both?

  • dave1707dave1707 Mod
    edited September 2016 Posts: 7,753

    return will cause an exit only from the function it's in.

    EDIT: It appears that any code after a return in the touched function still gets executed.

  • dave1707dave1707 Mod
    edited September 2016 Posts: 7,753

    From what I see with the return statement is:

    1: There can be one statement after a return statement but no more.
    2: That one statement will still be executed.
    3: If the return statement is in an if statement, then nothing after the if statement will be executed.

    @Simeon This doesn't seem right. No statements should be allowed after a return statement let alone be executed. If you try to uncomment the second text statement, it causes an error.

    function draw()
        background(40, 40, 50)
        fill(255)
        xx()
    end
    
    function xx()
        return
        text("statement 1 executed after a return",WIDTH/2,HEIGHT/2)
        --text("statement 2 executed after a return",WIDTH/2,HEIGHT/2)
    end
    
  • @dave1707, try returning a value with the return statement. For me at least, that makes the following line cause an error. I think the issue is just when return is called without anything after it, so returning nil.

  • edited September 2016 Posts: 110

    @dave1707 @TheSolderKing Maybe the return is returning the text function?

    EDIT:

    function draw()
        background(40, 40, 50)
        fill(255)
        xx()
    end
    
    function xx()
        if true then
            return --0
            text("statement 1 executed after a return",WIDTH/2,HEIGHT/2)
        end
        text("statement 2 executed after a return",WIDTH/2,HEIGHT/2)
    end
    

    Maybe return can only have one thing after it, then it must be an end. I guess this makes sense as you quit the function after returning a value, so you don't need anything after it.

  • AnatolyAnatoly Mod
    edited September 2016 Posts: 842

    The return function returns the argument after it till the "end"

    do return end
    

    The option here:

    do
        return text(arguments)
    end
    

    returns the text function, so calling the function where you place it, .. Blabla bla I'll just show it

    function a()
        return print("Test")
    end
    
    function setup()
        a() -- Is going to run the returned print function
    end
    
    

    You can return only unpacked arguments!!

    do
        return
        unpack(
          "Test", "Test 2"
        )
    end
    
    -- You have to call this by
    A, B = functionCall()
    

    My question was, if anything after end will be called, and it was answered.

Sign In or Register to comment.