SyntaxError: ‘return’ outside function in Python
We are given a problem of how to solve the 'Return Outside Function' Error in Python. So in this article, we will explore the 'Return Outside Function' error in Python. We will first understand what this error means and why it occurs. Then, we will go through various methods to resolve it with examples.
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. 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
- Common Mistake Alert
Syntax Error in Script
In this example, code attempts to iterate through a list of strings named "l" 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.
l = [ "The first line", "The second line", "The third line"]
for s in lines:
return s
Output
Hangup (SIGHUP)
File "Solution.py", line
return s
SyntaxError: 'return' outside function
Common Mistake Alert
In below code, the return sum statement is placed outside the scope of the function add_nums, resulting in the 'Return Outside Function' error.
def add_nums(a, b):
sum = a + b
# Return statement outside the function
return sum
Output
Hangup (SIGHUP)
File "Solution.py", line 6
return sum
^
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:
- Move 'return' inside a function
- Use 'print()' instead of return
- Use a 'Generator' (yield instead of return)
Move return inside a function
Since return must always be inside a function, we can define a function and place return within it to solve the error. Let's consider the example of the above code.
def get_first_line():
l = ["The first line", "The second line","The third line"]
for s in l:
return s # Now it's inside a function
print(get_first_line())
Output
The first line
Explanation: We defined a function get_first_line(), and now the return statement is valid.
Use print() instead of return
If we just need to display values instead of returning them, we can use print() instead of return to avoid getting this error.
l = ["The first line", "The second line", "The third line"]
for s in l:
print(s) # Prints each line without error
Output
The first line The second line The third line
Use a Generator (yield instead of return)
Instead of using return, which immediately exits a function, we can use yield to turn the function into a generator. This allows us to produce values one by one without breaking the loop. It's generally used when we need to return multiple values from a function, but it can also replace 'return' to return a single value.
def get_lines():
lines = ["The first line", "The second line", "The third line"]
for s in lines:
yield s
gen = get_lines()
for line in gen:
print(line)
Output
The first line The second line The third line
Explanation: since yield returns a generator instead of a final value, we must iterate over the generator (for line in gen) to retrieve values.
Output
The first line The second line The third line