Select Page

How to Solve Python SyntaxError: ‘return’ outside function

by | Programming, Python, Tips

blog banner for post titled: How to Solve Python SyntaxError: ‘return’ outside function

In Python, the return keyword ends the execution flow of a function and sends the result value to the main program. You must define the return statement inside the function where the code block ends. If you define the return statement outside the function block, you will raise the error “SyntaxError: ‘return’ outside function”.

This tutorial will go through the error in more detail, and we will go through an example scenario to solve it.

SyntaxError: ‘return’ outside function

What is a Syntax Error in Python?

Syntax refers to the arrangement of letters and symbols in code. A Syntax error means you have misplaced a symbol or a letter somewhere in the code. Let’s look at an example of a syntax error:

number = 45

print()number
    print()number
           ^
SyntaxError: invalid syntax

The ^ indicates the precise source of the error. In this case, we have put the number variable outside of the parentheses for the print function,

print(number)
45

The number needs to be inside parentheses to print correctly.

What is a Return Statement?

We use a return statement to end the execution of a function call and return the value of the expression following the return keyword to the caller. It is the final line of code in our function. If you do not specify an expression following return, the function will return None. You cannot use return statements outside the function you want to call. Similar to the return statement, the break statement cannot be outside of a loop. If you put a break statement outside of a loop you will raise SyntaxError: ‘break’ outside loop“. Let’s look at an example of incorrect use of the return statement.

Example: Return Statement Outside of Function

We will write a program that converts a temperature from Celsius to Fahrenheit and returns these values to us. To start, let’s define a function that does the temperature conversion.

# Function to convert temperature from Celsius to Fahrenheit

def temp_converter(temp_c):

    temp_f = (temp_c * 9 / 5) + 32

return temp_f

The function uses the Celsius to Fahrenheit conversion formula and returns the value. Now that we have written the function we can call it in the main program. We can use the input() function to ask the user to give us temperature data in Celsius.

temperature_in_celsius = float(input("Enter temperature in Celsius"))

temperature_in_fahrenheit = temp_converter(temperature_in_celsius)

Next, we will print the temperature_in_fahrenheit value to console

print(<meta charset="utf-8">temperature_in_fahrenheit)

Let’s see what happens when we try to run the code:

    return temp_f
    ^
SyntaxError: 'return' outside function

The code failed because we have specified a return statement outside of the function temp_converter.

Solution

To solve this error, we have to indent our return statement so that it is within the function. If we do not use the correct indentation, the Python interpreter will see the return statement outside the function. Let’s see the change in the revised code:

# Function to convert temperature from Celsius to Fahrenheit

def temp_converter(temp_c):

    temp_f = (temp_c * 9 / 5) + 32

    return temp_f
temperature_in_celsius = float(input("Enter temperature in Celsius"))

temperature_in_fahrenheit = temp_converter(temperature_in_celsius)

print(temperature_in_fahrenheit)
Enter temperature in Celsius10

50.0

The program successfully converts 10 degrees Celsius to 50 degrees Fahrenheit.

For further reading on using indentation correctly in Python, go to the article: How to Solve Python IndentationError: unindent does not match any outer indentation level.

Summary

Congratulations on reading to the end of this tutorial! The error: “SyntaxError: ‘return’ outside function” occurs when you specify a return statement outside of a function. To solve this error, ensure all of your return statements are indented to appear inside the function as the last line instead of outside of the function.

Here is some other SyntaxErrors that you may encounter:

Go to the online courses page on Python to learn more about Python for data science and machine learning.