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 functionThe 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 – 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_rateThe 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.

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
returnstatement is indented at the same level as the code within the function. - Double-check that your
returnstatement 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:
- Command errored out with exit status 1 in Python
- Python Except Keyerror
- How to fix the Python 3 pickle typeerror a bytes-like object is required not ‘str’
- NameError: name is not defined in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.