Explain me about return function

Godot Version

godot 3.5

Question

how this code work and how return function work
Ask your question here! Try to give as many details as possible

In this case, the return statement is used to exit a function early. For example:

if not body is Player: return
sprite.play("checked")

This means that if something other than the player enters the checkpoint’s area, the function will exit immediately and the checked animation won’t play. Only when the player enters does it continue and play the animation.

The return statement can also be used to send a value back from a function. If no value is specified, it returns null by default. Like this:

func _ready():
    var result = sum(2, 4)
    print(result)

func sum(a, b):
    var answer = a+b
    return answer

Here, the sum() function returns the result of a + b, which gets printed in the _ready() function.

In short, return is used to:

  1. Exit a function early
  2. Send a value back to wherever the function was called
3 Likes