How to break a function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

I want to break a function for example:

func _ready():
    if some_condition:
        print("true")
    else:
        # break
    print("test")

So I want it to be broke and doesn’t print “test”, only “true”.

:bust_in_silhouette: Reply From: SIsilicon

The break keyword in most programs, including Godot, stops the execution of an if statement or a for, while or any iterative loop. To ‘break’ out of a function, just use the return keyword.

func _ready():
    if some_condition:
        print("true")
    else:
        return # breaks execution of the entire function
    print("test")

Please note that according to your code fragment, this would either print out both true and test, or neither at all.

SIsilicon | 2019-02-24 14:16

Sorry if my replying here causes trouble but I want to point out a mistake
The answer above is not correct, maybe I’m a newbie or something, I don’t think break would stop execution of if statement: Does it?
If someone is learning coding and run into here, better double check somewhere else.

Maybe you are thinking of break keyword which breaks/exits a loop.

The OP example is clear, wants to end/exit the function someplace in the middle. And return is correct.