Syntaxerror: ‘return’ outside function

In this article, we’ll walk you through how to fix the Python syntaxerror: ‘return’ outside function.

This syntax error you’ll face when you’re running your code in Python.

If this error gives you a hard time, then keep on reading.

This article will show how to resolve this syntax error and give you a better understanding with regards to this error.

What is “return” statement?

The return statement is used to exit a function and return a value. It should only be used within the body of a function. Ensure that your return statement is indented correctly and is inside a function definition.

What is “syntaxerror return outside function”?

The syntaxerror: ‘return’ outside function occurs when the return statement is used outside the scope of a function.

This error indicates that the “return” statement is misplaced and does not have a corresponding function to be associated with.

For example:

def calculate_sum(x, y):
    sum = x + y

return sum  # This line causes the Syntaxerror: 'return' outside function

result = calculate_sum(10, 20)
print(result)

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py 
  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 4
    return sum  # This line causes the Syntaxerror: 'return' outside function
    ^^^^^^^^^^
SyntaxError: 'return' outside function

Since the “return” statements are only valid within functions, placing them elsewhere results in a syntax error.

Moreover, this syntax error is like an indentation error it occurs when the indent or return function does not match and align with the indent of the initialized function.

Why does the “syntaxerror: ‘return’ outside function” error occurs in Python?

This time we have to know the reason for this syntax error. Here are the several reasons why this error occurs in your Python code:

❌ When the “return” statement is mistakenly placed outside the body of a function or in the global scope.

❌ If the ‘return’ statement is not indented correctly, it may lead to an error.

❌ If the ‘return’ statement is present within a nested function and not enclosed within the function.

How to fix “syntaxerror: ‘return’ outside function”?

To fix the error, you have to make sure that the return statement is used only within the body of a function and identify the misplaced ‘return’ statement.

Check for any missing function declarations, inconsistent indentation, or accidental placement of ‘return’ statements outside of function scopes.

Here are the following solutions that you can use to fix the error:

Solution 1: Correct indentation of ‘return’ statement

def calculate_sum(x, y):
    sum = x + y
    return sum

result = calculate_sum(500, 120)
print(result)

As you can see in this example code, we correct the indentation of the “return” statement.
By indenting it properly to align with the function body, we ensure that the ‘return’ statement is placed within the function scope.

The code will run without any error, and the result will be printed as 620, which is the sum of 500 and 120.

Output:

620

Solution 2: Incorporate the ‘return’ statement within an if-else statement

def calculate_sum(x, y):
    sum = x + y

    if sum > 20:
        return sum
    else:
        return None

result = calculate_sum(50, 20)
print(result)

Hwew, incorporate the “return” statement within an if-else statement. This allows us to conditionally return the sum if it is greater than 20, or return None otherwise.

The code will run without any error, and the result will be printed as 70, as the sum of 50 and 20 is greater than 10.

Output:

70

Solution 3: Assign the sum to a global variable for later use

sum = None

def calculate_sum(x, y):
    global sum
    sum = x + y

calculate_sum(150, 780)
print(sum)

Here, we assign the sum to a global variable named sum inside the calculate_sum() function using the global keyword. This allows us to access the sum value outside the function.
The code will run without any error, and the result will be printed as 930, as the global variable holds the calculated sum.

Output:

930

Conclusion

In conclusion, the error message syntaxerror: ‘return’ outside function occurs when the return statement is used outside the scope of a function.

We already discussed above what this error is all about and multiple ways to resolve this error.

By executing the solutions above you can master this Syntaxerror in Python with the help of this guide.

You could also check out other “SyntaxError” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment