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 😊

Leave a Comment