The
return
keyword in Python is used to end the execution flow of the function and send the result value to the main program. The return statement must be defined inside the function when the function is supposed to end. But if we define a return statement outside the function block, we get the
SyntaxError: 'return' outside function
Error.
In this Python guide, we will explore this Python error and discuss how to solve it. We will also see an example scenario where many Python learners commit this mistake, so you could better understand this error and how to debug it. So let's get started with the Error Statement.
Python Error: SyntaxError: 'return' outside function
The Error Statement
SyntaxError: 'return' outside function
is divided into two parts. The first part is the Python Exception
SyntaxError
, and the second part is the actual Error message
'return' outside function
.
-
SyntaxError
: SyntaxError occurs in Python when we write a Python code with invalid syntax. This Error is generally raised when we make some typos while writing the Python program. -
'return' outside function
: This is the Error Message, which tells us that we are using thereturn
keyword outside the function body.
Error Reason
The Python
return
is a reserved keyword used inside the function body when a function is supposed to return a value. The return value can be accessed in the main program when we call the function. The
return
keyword is exclusive to Python functions and can only be used inside the function's local scope. But if we made a typo by defining a return statement outside the function block, we will encounter the "SyntaxError: 'return' outside function" Error.
For example
Let's try to define a return statement outside the function body, and see what we get as an output.
# define a function
def add(a,b):
result = a+b
# using return out of the function block(error)
return result
a= 20
b= 30
result = add(a,b)
print(f"{a}+{b} = {result}")
Output
File "main.py", line 6
return result
^
SyntaxError: 'return' outside function
Break the code
We are getting the "SyntaxError: 'return' outside function" Error as an output. This is because, in line 6, we are using the
return result
statement outside the function, which is totally invalid in Python. In Python, we are supposed to use the return statement inside the function, and if we use it outside the function body, we get the SyntaxError, as we are encountering in the above example. To solve the above program, we need to put the return statement inside the function block.
solution
# define a function
def add(a,b):
result = a+b
# using return inside the function block
return result
a= 20
b= 30
result = add(a,b)
print(f"{a}+{b} = {result}")
Common Scenario
The most common scenario where many Python learners encounter this error is when they forget to put the indentation space for the
return
statement and write it outside the function. To write the function body code, we use indentation and ensure that all the function body code is intended. The
return
statement is also a part of the function body, and it also needs to be indented inside the function block.
In most cases, the
return
statement is also the last line for the function, and the coder commits the mistake and writes it in a new line without any indentation and encounters the SyntaxError.
For example
Let's write a Python function
is_adult()
that accept the
age
value as a parameter and return a boolean value
True
if the age value is equal to or greater than 18 else, it returns False. And we will write the
return
Statements outside the function block to encounter the error.
# define the age
age = 22
def is_adult(age):
if age >= 18:
result = True
else:
result = False
return result #outside the function
result = is_adult(age)
print(result)
Output
File "main.py", line 9
return result #outside the function
^
SyntaxError: 'return' outside function
Break The code
The error output reason for the above code is pretty obvious. If we look at the output error statement, we can clearly tell that the
return result
statement is causing the error because it is defined outside the
is_adult()
function.
Solution
To solve the above problem, all we need to do is put the return statement inside the
is_adult()
function block using the indentation.
Example Solution
# define the age
age = 22
def is_adult(age):
if age >= 18:
result = True
else:
result = False
return result #inside the function
result = is_adult(age)
print(result)
Output
True
Final Thoughts!
The Python
'return' outside function
is a common Python SyntaxError. It is very easy to find and debug this error. In this Python tutorial, we discussed why this error occurs in Python and how to solve it. We also discussed a common scenario when many python learners commit this error.
You can commit many typos in Python to encounter the SyntaxError, and the 'return' outside the Function message will only occur when you define a return statement outside the function body. If you are still getting this error in your Python program, please share your code in the comment section. We will try to help you in debugging.
People are also reading:
- What is Python used for?
- Python TypeError: ‘float’ object cannot be interpreted as an integer Solution
- How to run a python script?
- Python TypeError: ‘NoneType’ object is not callable Solution
- Python Features
- How to Play sounds in Python?
- Python Interview Questions
- Python typeerror: list indices must be integers or slices, not str Solution
- Best Python IDEs
- Read File in Python
Leave a Comment on this Post