Menu

GDScript Functions: func, Parameters, Defaults, return

Define and call functions in GDScript: parameters with types and default values, return values, returning several values, recursion, variadic functions and the virtual functions Godot calls for you.

This page includes runnable editors. Edit, run, and see output instantly.

A function is a named block of code you can run again and again. Define it with func and call it by name:

Welcome, Ana
Welcome, Bo

A script's functions are its methods: they belong to the node, and they can read every member variable of the script. The order does not matter, so greet may be defined after _ready.

Parameters, Types and return

Add types to the parameters and the return value with ->. return sends a value back and ends the function:

18
true
22

A function without return, or declared -> void, returns null. See static typing for what typed functions catch.

Default Values

A parameter with = value is optional:

1 x slime
5 x bat
1 x dragon (boss)

Arguments are matched by position, and GDScript has no named arguments. To set boss you must also pass count. When a function grows many options, take a dictionary of settings instead.

Returning Several Values

Return an array or a dictionary:

low 1, high 9
21 5.25

Recursion

A function may call itself. Give it a case that stops:

120

Calls can only nest so deep: by default GDScript fails at about 1,000 nested calls. Walk large trees with a loop and an array instead of recursion.

Variadic Functions

Since Godot 4.5, a last parameter written ...name collects any number of extra arguments into an array:

6
0

Functions Are Values

A function's name without parentheses is a Callable, a value you can store, pass and call later. This is how signals and sort_custom() take functions:

RUN!
JUMP!

The lambdas and Callables page goes further: inline functions, bind() and captured variables.

Functions Godot Calls for You

Functions that start with _ and match an engine name are virtual: you define them, Godot calls them.

FunctionCalled
_init()when the object is created
_ready()once, when the node and its children are in the scene
_process(delta)every frame
_physics_process(delta)every physics step, 60 times a second by default
_input(event), _unhandled_input(event)on every input event
_exit_tree()when the node leaves the scene

Your own private helpers often start with _ too, by convention. That is only a naming habit: GDScript has no private keyword.

Frequently Asked Questions

How do I define a function in GDScript?

Write func name(parameters): and indent the body under it. Add types and a return type for checking: func heal(amount: int) -> void:. Call it by name: heal(10).

How do I give a parameter a default value in GDScript?

Write = value after the parameter: func greet(name: String, greeting: String = "Hello"). Parameters with defaults must come after those without, and callers may leave them out.

Can a GDScript function return more than one value?

Not directly. Return an array (return [low, high]) or a dictionary (return {"low": low, "high": high}) and read its parts. A dictionary keeps the names, so it is clearer for more than two values.

Does GDScript support named arguments?

No. Arguments are matched by position. For a function with many optional settings, pass a dictionary of options instead.

What do functions starting with an underscore mean in Godot?

By convention, a leading underscore marks a function as private to the script. Godot's own virtual functions, which the engine calls for you, also start with one: _ready(), _process(), _physics_process(), _input().

Coddy programming languages illustration

Learn to code with Coddy

Get started