Skip to reading
GDScript Programming & Godot

13. Functions, defaults and return values

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
func total_price(price: float, count: int = 1) -> float:
    return price * count
Code example
func run() -> Array:
    return [total_price(2.5), total_price(2.5, 3)]

A function has a name, a parameter list and an optional return type. Default arguments make trailing parameters optional. -> void means the function does not return a value. A bare return leaves it without a result.

Use ordinary positional arguments when calling GDScript functions. An options dictionary is still one argument, even when it contains many named fields. Required parameters must come before optional ones.

PCA normally calls run when the record is run directly. If run accepts at least one parameter and no arguments were supplied, PCA passes the caller context as its first argument. The same rule applies to on_* handlers. A named utility function receives the argument array supplied by its caller.

A local called call_context or event avoids shadowing the inherited context member. Do not override an inherited helper such as Action or CallScript with an incompatible signature.

Page 21 of 378Permalink
Go to page