[Solved] SyntaxError: ‘return’ outside function in Python
Tags: python, syntaxerror, programming
Published: 2023-02-01 15:19:26

## 🚫 SyntaxError: ‘return’ outside function

> This post was originally published on [decodingweb.dev](https://www.decodingweb.dev/solved-syntaxerror-unterminated-triple-quoted-string-literal-python) as part of the [Python Syntax Errors series](https://www.decodingweb.dev/category/python-syntaxerror).

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](https://www.decodingweb.dev/how-to-learn-a-programming-language#language-syntax-and-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.

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)

## How to fix SyntaxError: 'return' outside function

This SyntaxError happens under various scenarios:

<u>1. Inconsistent indentation</u>
<u>2. Using the return statement to break out of a loop</u>
<u>3. Let's explore each scenario with some examples.</u>

**1. 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.

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


```python
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:


```python
# ✅ 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:


```python
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.

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


```python
# ✅ 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:

```python
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:


```python
# ✅ Correct
items = range(1, 100)

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

And that's how you'd fix the error "SyntaxError: 'return' outside function" in Python.

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.