Return (Function)
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
return is a keyword that ends a function and sends a value back to the caller.
In GDScript, you can return any value from a function, but this return value should match the return type declared in the function's signature.
For example, this function returns an integer:
func return_five() -> int:
return 5But if I wrote this, Godot would give me an error:
func return_five() -> int:
return "five"Because "five" is a string, not an integer.
This function is an error too. Can you spot it?
var should_return_five := true
func return_five() -> int:
if should_return_five == true:
return 5
else:
return nullThe declared return type is an int, but the effective return type is a mix of either int or null. The two don't match. To make the function valid, I could return 0 in the else branch.
This example is obvious, but there are cases where Godot might not be able to tell if a function's return matches the signature, even if you're able to. Consider the below:
var default_return = 0
func return_five() -> int:
if some_condition:
return 5
return default_returnIn this case, Godot can't tell if the function will always return an integer because default_return is not typed. While you know it's an int, there's no guarantee it won't change. If you're sure it won't, you can add a type hint:
func return_five() -> int:
if some_condition:
return 5
return default_return as intThe as keyword casts the variable. It's a way to tell Godot "I know what I'm doing, trust me".
This is just a simplified example; in this case, typing default_return would be a clearly better solution than using as.
While both mathematical functions and programming functions share the same name, they are not the same thing.
A function can indeed work like a math function:
func multiply_by_ten(number: int) -> int:
return number * 10That's what we call a pure function, because it is self-contained. It doesn't use anything beside its arguments.
But it can also affect an external variable, without returning anything:
var counter := 0
func increment_counter(number: int) -> void:
counter += numberAffecting the world around is what we call a side effect. Notice the lack of return in this function; it does something, but doesn't return anything.
Of course, there's nothing stopping us from writing a function that does both. This function multiplies by 10, returns the result, but also counts how many times it was called:
var counter := 0
func multiply_by_ten(number: int) -> int:
counter += 1
return number * 10This is a function with both side effects (affects the world around it) and a return value. These functions are very common.
Don't stop here. Step-by-step tutorials are fun but they only take you so far.
Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…