Early Returns

2025/12/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
Assets
All else
Copyright 2016-2026, GDQuest
Created
2026/02/16
Updated
2025/12/27

Early Returns

Some functions need to check various conditions before running their instructions. Suppose you have a function that does complex calculations when the player gets hit. Something like:

func player_hit(damage: int) -> void:
  if player_health > 0:
    if player_is_invincible == false:
      player_health -= damage

Early returns allow you to avoid nesting:

func player_hit(damage: int) -> void:
  if player_health < 0:
    return
  if player_is_invincible == true:
    return
  player_health -= damage

In this example, if anything prevents the function from functioning, it will return, interrupting itself. Not only does this avoid deeply nesting if statements, but it also makes mistakes a little less likely.

This example is very straightforward, but in longer pieces of code, it often happens that you write a piece of code outside the boundary of the conditional by mistake. Early returns make that type of mistake impossible.

Become an Indie Gamedev with GDQuest!

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.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!