Python SyntaxError: ‘return’ outside function Solution
A return statement sends a value from a function to a main program. If you specify a return statement outside of a function, you’ll encounter the “SyntaxError: ‘return’ outside function” error.
In this guide, we explore what the “‘return’ outside function” error means and why it is raised. We’ll walk through an example of this error so you can figure out how to solve it in your program.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
Select your interest
First name
Last name
Phone number
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
SyntaxError: ‘return’ outside function
Return statements can only be included in a function. This is because return statements send values from a function to a main program. Without a function from which to send values, a return statement would have no clear purpose.
Return statements come at the end of a block of code in a function. Consider the following example:
Our return statement is the final line of code in our function. A return statement may be used in an if statement to specify multiple potential values that a function could return.
An Example Scenario
We’re going to write a program that calculates whether a student has passed or failed a computing test. To start, let’s define a function that checks whether a student has passed or failed. The pass-fail boundary for the test is 50 marks.
Our function can return two values: True or False. If a student’s grade is over 50 (above the pass-fail boundary), the value True is returned to our program. Otherwise, the value False is returned. Our program prints the value “Checked” no matter what the outcome of our if statement is so that we can be sure a grade has been checked.
Now that we have written this function, we can call it in our main program. First, we need to ask the user for the name of the student whose grade the program should check, and for the grade that student earned. We can do this using an input() statement:
The value of “grade” is converted to an integer so we can compare it with the value 50 in our function. Let’s call our function to check if a student has passed their computing test:
We call the check_if_passed() function to determine whether a student has passed their test. If the student passed their test, a message is printed to the console telling us they passed; otherwise, we are informed the student failed their test.
Let’s run our code to see if it works:
An error is returned.
The Solution
We have specified a return statement outside of a function. Let’s go back to our check_if_passed() function. If we look at the last line of code, we can see that our last return statement is not properly indented.
The statement that returns False appears after our function, rather than at the end of our function. We can fix this error by intending our return statement to the correct level:
The return statement is now part of our function. It will return the value False if a student’s grade is not over 50. Let’s run our program again:
Our program successfully calculates that a student passed their test.
Conclusion
The “SyntaxError: ‘return’ outside function” error is raised when you specify a return statement outside of a function. To solve this error, make sure all of your return statements are properly indented and appear inside a function instead of after a function.
Now you have the knowledge you need to fix this error like an expert Python programmer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
SyntaxError: ‘Return’ Outside Function in Python
This syntax error is nothing but a simple indentation error, generally, this error occurs when the indent or return function does not match or align to the indent of the defined function.
Example
As you can see that line no. 7 is not indented or align with myfunction(), due to this python compiler compile the code till line no.6 and throws the error ‘return statement is outside the function.
Correct Example
Conclusion
We have understood that indentation is extremely important in programming. As Python does not use curly braces like C, indentation and whitespaces are crucial. So, when you type in the return statement within the function the result is different than when the return statement is mentioned outside. It is best to check your indentation properly before executing a function to avoid any syntax errors.
Python SyntaxError :'return' outside function
I would check my indentation, it looks off. Are you possibly mixing tabs and spaces? The PEP8 (Python Style Guide) recommends using 4 spaces only. Unlike other languages, whitepace makes a big difference in Python, so consistency is important.
The above also makes the following recommendation:
When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
In particular, your 2nd else seems to be off (probably should be indented), and this method def __div__(self, other): too (which I would think ought to be at the same level as your other def s — i.e., moved «out» rather than indented).
Problems mixing tabs/blanks are easy to have since both characters are «invisible».
Python SyntaxError: ‘return’ outside function Solution
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 the return 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.
Output
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
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.
Output
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
Output
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.