Open In App

SyntaxError: ‘return’ outside function in Python

We are given a problem of how to solve the ‘Return Outside Function’ Error in Python. In this article, we will see how to solve the ‘Return Outside Function’ Error in Python with reasons for it’s occurring and also approaches to solve SyntaxError: ‘return’ outside function” in Python.

What is SyntaxError: ‘return’ outside function in Python?

The ‘Return Outside Function’ error is raised by the Python interpreter when it encounters a return statement outside the scope of a function. In Python, the return statement is used to exit a function and return a value to the caller. If this statement is mistakenly placed outside a function, Python will throw this error.



Syntax:

SyntaxError: 'return' outside function 

Below are the reasons by which SyntaxError: ‘return’ outside function in Python occurs:



Syntax Error in Script

In this example, code attempts to iterate through a list of strings named “lines” and use a return statement within a for loop, but it will result in a ‘Return Outside Function’ error because the return statement is not inside a function.




statement= ["The simple statement is of the example for the outside function",
             " The second line",
            "The third line",
                      ]
for statement in lines:
    return statement

Output

Hangup (SIGHUP)
  File "Solution.py", line 6
    return statement
SyntaxError: 'return' outside function

Common Mistake Alert

In below code, the return sum_result statement is placed outside the scope of the function add_numbers, resulting in the ‘Return Outside Function’ error.




def add_numbers(a, b):
    sum_result = a + b
 
# Return statement outside the function
return sum_result

Output

Hangup (SIGHUP)
  File "Solution.py", line 6
    return sum_result
    ^
SyntaxError: 'return' outside function

Solution for SyntaxError: ‘return’ outside function in Python

Below are some of the approaches to solve SyntaxError: ‘return’ outside function in Python:

Iterating through Statement

Below, code is corrected by replacing “lines” with “statement” in the for loop, and the return statement is moved outside the loop to prevent a ‘Return Outside Function’ error. This ensures proper iteration through the list of statements without violating Python syntax.




statements = ["The simple statement is of the example for the outside function",
              "The second line",
              "The third line"
              ]
 
for statement in statements:
    print(statement)

Output
The simple statement is of the example for the outside function
The second line
The third line

Correct the Indentation

For avoid the ‘Return Outside Function’ Error correct the indentation of the code and call the function properly by taking valid input .




def add_numbers(a, b):
    sum_result = a + b
    return sum_result
 
a = 2
b = 3
print(add_numbers(a, b))

Output
5

Conclusion

In conclusion , the ‘Return Outside Function’ error in Python serves as a reminder of the language’s strict syntax rules. By understanding the reasons behind this error and following the suggested approaches for resolution, developers can enhance their coding practices and create more robust and error-free Python scripts. Paying attention to proper indentation, function definitions, and careful placement of return statements.


Article Tags :