syntaxerror ‘return’ outside function Python error [Causes & How to Fix]

In this Python tutorial, I will show you, how to fix, syntaxerror ‘return’ outside function python error with a few examples.

The ‘SyntaxError: ‘return’ outside function’ is quite common in Python, especially among beginners. This error is self-explanatory: it indicates that a ‘return’ statement has been used outside of a function. In this tutorial, we will deep dive into why this error occurs and provide methods to fix it.

Understanding ‘return’ Statement in Python

To understand the error, it’s first essential to comprehend the role of the ‘return’ statement in Python. The ‘return’ statement is used in a function to end the execution of the function call and ‘returns’ the result (value) to the caller. The statements after the ‘return’ statement are not executed.

Here’s an example:

def add_numbers(num1, num2):
    result = num1 + num2
    return result

print(add_numbers(5, 7))  # Outputs: 12

In this code, we have a function called ‘add_numbers’ that adds two numbers and uses the ‘return’ statement to send the result back to the caller.

Read: How to fix IndexError list out of Range Error in Python

What Causes ‘SyntaxError: ‘return’ Outside Function’ Error?

As the error message suggests, it occurs when the ‘return’ statement is used outside of a function. In Python, ‘return’ can only be used in the definition of a function. Using it elsewhere, like in loops, conditionals, or just on its own, is not allowed and will result in a SyntaxError.

Here is an example that will throw this error. Let’s say you have a list of grades and you want to calculate the average grade. You might write something like this:

grades = [85, 90, 78, 92, 88]

total = sum(grades)
average = total / len(grades)

return average

f you run this script, it will throw a SyntaxError: 'return' outside function, because you’re using return in the main body of the script instead of within a function.

You can see the error message:

syntaxerror- 'return' outside function

Read: How to use the range() function to iterate through a list in Python

How to Fix ‘SyntaxError: ‘return’ Outside Function’ Error?

The fix for this error is quite straightforward: ensure that the ‘return’ statement is used only inside function definitions.

In case you’re trying to use ‘return’ in your main script, consider whether you really want to return a value. If you’re attempting to output a value to the console, use the ‘print()’ function instead. If you’re trying to end the execution of your script, consider using ‘sys.exit()’.

Here’s how you can fix the error shown in the previous section:

def calculate_average(grades):
    total = sum(grades)
    average = total / len(grades)
    return average

grades = [85, 90, 78, 92, 88]

print(calculate_average(grades))  # Outputs: 86.6

In this corrected code, we’ve created a function named calculate_average that takes a list of grades, calculates the average, and then returns it. Now, the return statement is correctly placed inside a function, so there’s no SyntaxError. Finally, we call this function and print the returned value.

Conclusion

The ‘SyntaxError: ‘return’ outside function’ error in Python can be avoided by ensuring that ‘return’ statements are used appropriately within the body of function definitions only.

You may also like: