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.
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:
def add_two_numbers(x, y): answer = x + y return answer
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.
def check_if_passed(grade): if grade > 50: print("Checked") return True else: print("Checked") return False
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:
name = input("Enter the student's name: ") grade = int(input("Enter the student's grade: "))
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:
has_passed = check_if_passed(grade) if has_passed == True: print("{} passed their test with a grade of {}.".format(name, grade)) else: print("{} failed their test with a grade of {}.".format(name, grade))
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:
File "test.py", line 6 return False ^ SyntaxError: 'return' outside function
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.
… else: print("Checked") return False
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:
else: print("Checked") return False
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:
Enter the student's name: Lisa Enter the student's grade: 84 Checked Lisa passed their test with a grade of 84.
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.