How to fix “SyntaxError: ‘return’ outside function” in Python 

Updated Oct 22, 2023 ⤳ 3 min read

Python raises the error “SyntaxError: ‘return’ outside function” once it encounters a return statement outside a function.

Here’s what the error looks like:


File /dwd/sandbox/test.py, line 4
  return True
  ^^^^^^^^^^^
SyntaxError: 'return' outside function

Based on Python's syntax & semantics, a return statement may only be used in a function to return a value to the caller. 

However, if - for some reason - a return statement isn't nested in a function, Python's interpreter raises the "SyntaxError: 'return' outside function" error.

You might like:

Using the return statement outside a function isn't something you'd do on purpose, though; This error usually happens when the indentation-level of a return statement isn't consistent with the rest of the function.

Additionally, it can occur when you accidentally use a return statement to break out of a loop (rather than using the break statement)

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

How to fix the "'return' outside function" error?

Python return outside function error happens under various scenarios including:

  1. Inconsistent indentation
  2. Using the return statement to break out of a loop

Let's explore each scenario with some examples.

Inconsistent indentation: A common cause of this syntax error is an inconsistent indentation, meaning Python doesn't consider the return statement a part of a function because its indentation level is different.

You might like:

In the following example, we have a function that accepts a number and checks if it's an even number:


# 🚫 SyntaxError: 'return' outside function
def isEven(value):
  remainder = value % 2
  
  # if the remainder of the division is zero, it's even
return remainder == 0

As you probably noticed, we hadn't indented the return statement relative to the isEven() function.

To fix it, we correct the indentation like so:


# ✅ Correct
def isEven(value):
  remainder = value % 2
  
  # if the remainder of the division is zero, it's even
  return remainder == 0

Problem solved!

Let's see another example:


# 🚫 SyntaxError: 'return' outside function
def check_age(age):
  print('checking the rating...')

# if the user is under 12, don't play the movie
if (age < 12):
  print('The movie can\'t be played!')
  return

In the above code, the if block has the same indentation level as the top-level code. As a result, the return statement is considered outside the function.

You might like:

To fix the error, we bring the whole if block to the same indentation level as the function.


# ✅ Correct
def check_age(age):
  print('checking the rating...')
  
  # if the user is under 12, don't play the movie
  if (age < 12):
    print('The movie can\'t be played!')
    return

  print('Playing the movie')

check_age(25)
# output: Playing the movie

Using the return statement to break out of a loop: Another reason for this error is using a return statement to stop a for loop located in the top-level code.

The following code is supposed to print the first fifteen items of a range object:


# 🚫 SyntaxError: 'return' outside function
items = range(1, 100)

# print the first 15 items
for i in items:
  if i > 15:
    return
  print(i)

However, based on Python's semantics, the return statement isn't used to break out of functions - You should use the break statement instead:


# ✅ Correct
items = range(1, 100)

# print the first 15 items
for i in items:
  if i > 15:
    break
  print(i)

In conclusion, always make sure the return statement is indented relative to its surrounding function. Or if you're using it to break out of a loop, replace it with a break statement.

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version