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:
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!in GDSchool
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.