SyntaxError: ‘return’ outside function means Python found a return line that isn’t inside a def block. The fix is to indent it under the function, or, if there’s no function, replace it with break, print() or a variable:
def ship(total):
if total > 50:
return "free shipping" # indented under def: works
return "standard"
In this tutorial, I’ll reproduce the error five ways and fix each one, all run on Python 3.12.5 in a Windows Command Prompt.
Why does Python raise SyntaxError: ‘return’ outside function?
return hands a value back to whoever called a function, so it only makes sense inside one. My Python return statement guide covers how it works when it’s in the right place. Here’s a script that uses it at the top level:
print("Checking cart total...")
total = 84.50
if total > 50:
return "free shipping"
Output:
File "C:\pyguides\cart_check.py", line 4
return "free shipping"
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'return' outside function

return on line 4, and the print() on line 1 never runs.You can see the first line didn’t print. It’s a compile-time error, so Python rejects the whole file before running anything.
I ran the same file on Python 3.14.8 and got the identical message, so the wording hasn’t changed between 3.12 and 3.14.
Fix 1: indent return under the def line
This is the cause I see most often: one line slips back to column 0. If you move blocks around a lot, see indenting multiple lines in Python help:
def sales_tax(price, rate=0.0825):
tax = round(price * rate, 2)
return tax
print(sales_tax(129.99))
Output:
File "C:\pyguides\sales_tax.py", line 3
return tax
^^^^^^^^^^
SyntaxError: 'return' outside function

return tax line sits at column 0, so it’s no longer part of sales_tax().Line it up with the function body and your call works:
def sales_tax(price, rate=0.0825):
tax = round(price * rate, 2)
return tax # same indent as the line above
print(sales_tax(129.99))
Output:
10.72
Fix 2: use break instead of return in a loop
You wanted to stop a loop early and typed return out of habit. With no function around the loop, Python fails on line 4:
orders = [42.00, 18.75, 310.00, 64.25]
for amount in orders:
if amount > 300:
return amount
Output:
File "C:\pyguides\big_orders.py", line 4
return amount
^^^^^^^^^^^^^
SyntaxError: 'return' outside function
Use break to leave the loop, as explained in Python break and continue, or wrap the loop in a function so return is legal again:
orders = [42.00, 18.75, 310.00, 64.25]
# Fix 1: stay at module level and use break
for amount in orders:
if amount > 300:
print(f"break: flagged ${amount:.2f}")
break
# Fix 2: move the loop into a function and return
def first_big_order(amounts, limit=300):
for amount in amounts:
if amount > limit:
return amount
return None
print("return:", first_big_order(orders))
print("return:", first_big_order(orders, limit=500))
Output:
break: flagged $310.00
return: 310.0
return: None

break stops at the $310.00 order; the function returns it, or None when nothing passes the limit.You can use either. For the other ways out of a function, see how to exit a function in Python.
Fix 3: move return out of a class body into a method
Your class body isn’t a function, even though it’s indented under class. A stray return there fails the same way:
class Cart:
items = ["laptop stand", "USB-C hub"]
return items
Output:
File "C:\pyguides\cart.py", line 3
return items
^^^^^^^^^^^^
SyntaxError: 'return' outside function
Put the logic in a method and return from that. A method is still a function, so the rules from defining a function in Python apply:
class Cart:
def __init__(self):
self.items = ["laptop stand", "USB-C hub"]
def get_items(self):
return self.items # return lives inside a method
print(Cart().get_items())
Output:
['laptop stand', 'USB-C hub']
Return outside function in Jupyter notebooks and the Python REPL
A Jupyter cell and the >>> prompt both run code at the top level, so a bare return raises this same SyntaxError there.
In a notebook, make the value the cell’s last expression and Jupyter displays it; to reuse the logic, put it in a def.
If you only need a placeholder body while you sketch a function, use pass instead of return; see the Python pass statement in functions.
How do I find a return outside function before running the script?
The built-in compile() raises the same SyntaxError without running anything, and it gives you the line number:
snippets = {
"module level": "total = 84.50\nif total > 50:\n return 'free shipping'",
"class body": "class Cart:\n items = []\n return items",
"inside def": "def ship(total):\n return total > 50",
}
for label, src in snippets.items():
try:
compile(src, label, "exec")
print(f"{label:<13} OK")
except SyntaxError as err:
print(f"{label:<13} line {err.lineno}: {err.msg}")
Output:
module level line 3: 'return' outside function
class body line 3: 'return' outside function
inside def OK

return inside a def passes.From the command line, python -m py_compile cart_check.py does the same check on a file. If the message is a plain invalid syntax instead, read how to fix invalid syntax in Python.
Once this error is gone for good, these function topics are worth a look:
- Get the name of a function in Python
- Pass a tuple as an argument to a function
- Python variable scope
- Fix UnboundLocalError in Python
Frequently asked questions
What does SyntaxError: ‘return’ outside function mean?
A return sits outside any function body, usually because of indentation.
Does any code run before the error?
No. It’s raised while Python compiles the file, so even the lines above the return don’t run.
Can I use return inside a loop?
Only if the loop itself is inside a function. At module level, use break instead.
Why do I get this error in a Jupyter notebook?
Cells run as top-level code. Leave the value as the last expression, or wrap it in a function.
Is there a similar error for yield?
Yes, SyntaxError: 'yield' outside function, with the same fix.
Where are the return rules documented?
See the return statement in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn