syntaxerror return outside function error in Python

In this tutorial, I will explain how to resolve the common Python error: SyntaxError: 'return' outside function. I will explain the causes of this error and provide detailed examples to help you understand and fix it effectively.

syntaxerror: ‘return’ outside function

The SyntaxError: 'return' outside function in Python occurs when a return statement is placed outside of a function. In Python, the return statement is used to exit a function and optionally pass an expression back to the caller. If Python encounters a return statement outside a function, it raises a SyntaxError because it doesn’t know how to handle it.

Example of syntaxerror return outside function

Let me show you where I got this error while doing some Python programming.

Here is the Python code snippet:

def calculate_sales_tax(amount):
    tax_rate = 0.07
return amount * tax_rate

# Calling the function
if __name__ == "__main__":
    purchase_amount = 100.00  # Example amount in USD
    sales_tax = calculate_sales_tax(purchase_amount)
    print(f"The sales tax for an amount of ${purchase_amount} is ${sales_tax:.2f}")

When you run the above code using VS code, you will get some error like below.

SyntaxError: 'return' outside function

The return statement is not indented correctly, causing Python to interpret it as being outside the function.

You can see the exact error screenshot below after executing the code above.

syntaxerror return outside function

syntaxerror: ‘return’ outside function – Fixes

To fix this error, ensure that the return statement is properly indented within the function. Here’s the corrected version of the previous example:

def calculate_sales_tax(amount):
    tax_rate = 0.07
    return amount * tax_rate

The complete code looks like below:

def calculate_sales_tax(amount):
    tax_rate = 0.07
    return amount * tax_rate

# Calling the function
if __name__ == "__main__":
    purchase_amount = 100.00  # Example amount in USD
    sales_tax = calculate_sales_tax(purchase_amount)
    print(f"The sales tax for an amount of ${purchase_amount} is ${sales_tax:.2f}")

Now, the return statement is inside the calculate_sales_tax function, and the code will execute without errors.

When I executed the above code, now I did not get any error. Here is the output screenshot below.

return outside function python

Here are a few things you should remember while working with this error:

  • Python relies heavily on indentation to define the scope of functions, loops, and conditionals. Ensure your return statement is indented at the same level as the code within the function.
  • Double-check that your return statement is within the function’s body and not outside any loops or conditionals meant to be part of the function.

Conclusion

The SyntaxError: 'return' outside function is a common error in Python, but it can be easily resolved by ensuring the return statement is properly indented within the function. I hope this tutorial helps you fix the error: syntaxerror: ‘return’ outside function.

You may also like the following errors:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.