fix syntaxerror 'return' outside function

How to Fix – SyntaxError: return outside function

The “SyntaxError: return outside function” error occurs when you try to use the return statement outside of a function in Python. This error is usually caused by a mistake in your code, such as a missing or misplaced function definition or incorrect indentation on the line containing the return statement.

fix syntaxerror 'return' outside function

In this tutorial, we will look at the scenarios in which you may encounter the SyntaxError: return outside function error and the possible ways to fix it.

What is the return statement?

A return statement is used to exit a function and return a value to the caller. It can be used with or without a value. Here’s an example:

# function that returns if a number is even or not
def is_even(n):
    return n % 2 == 0

# call the function
res = is_even(6)
# display the result
print(res)

Output:

True

In the above example, we created a function called is_even() that takes a number as an argument and returns True if the number is even and False if the number is odd. We then call the function to check if 6 is odd or even. We then print the returned value.

Why does the SyntaxError: return outside function occur?

The error message is very helpful here in understanding the error. This error occurs when the return statement is placed outside a function. As mentioned above, we use a return statement to exit a function. Now, if you use a return statement outside a function, you may encounter this error.

The following are two common scenarios where you may encounter this error.

1. Check for missing or misplaced function definitions

The most common cause of the “SyntaxError: return outside function” error is a missing or misplaced function definition. Make sure that all of your return statements are inside a function definition.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

For example, consider the following code:

print("Hello, world!")
return 0

Output:

Hello, world!

  Cell In[50], line 2
    return 0
    ^
SyntaxError: 'return' outside function

This code will produce the “SyntaxError: return outside function” error because the return statement is not inside a function definition.

To fix this error, you need to define a function and put the return statement inside it. Here’s an example:

def say_hello():
    print("Hello, world!")
    return 0

say_hello()

Output:

Hello, world!

0

Note that here we placed the return statement inside the function say_hello(). Note that it is not necessary for a function to have a return statement but if you have a return statement, it must be inside a function enclosure.

2. Check for indentation errors

Another common cause of the “SyntaxError: return outside function” error is an indentation error. In Python, indentation is used to indicate the scope of a block of code, such as a function definition.

Make sure that all of your return statements are indented correctly and are inside the correct block of code.

For example, consider the following code:

def say_hello():
    print("Hello, world!")
return 0

say_hello()

Output:

  Cell In[52], line 3
    return 0
    ^
SyntaxError: 'return' outside function

In the above example, we do have a function and a return statement but the return statement is not enclosed insdie the function’s scope. To fix the above error, indent the return statement such that it’s correctly inside the say_hello() function.

def say_hello():
    print("Hello, world!")
    return 0

say_hello()

Output:

Hello, world!

0

Conclusion

The “SyntaxError: return outside function” error is a common error in Python that is usually caused by a missing or misplaced function definition or an indentation error. By following the steps outlined in this tutorial, you should be able to fix this error and get your code running smoothly.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top