Syntaxerror return outside function python

By: On:

In this Python tutorial, we will discuss to fix an error, syntaxerror return outside function python. The error return outside function python comes while working with function in python.

Syntaxerror return outside function python

In python, this error can come when the indentation or return function does not match.

Example:


def add(x, y):
  sum = x + y
return(sum)
print(" Total is: ", add(20, 50))

After writing the above code (syntaxerror return outside function python), Ones you will print then the error will appear as a “ SyntaxError return outside function python ”. Here, line no 3 is not indented or align due to which it throws an error ‘return’ outside the function.

You can refer to the below screenshot syntaxerror return outside function python

Syntaxerror return outside function python
SyntaxError: return outside function python

To solve this SyntaxError: return outside function python we need to check the code whether the indentation is correct or not and also the return statement should be inside the function so that this error can be resolved.

Example:


def add(x, y):
  sum = x + y
  return(sum)
print(" Total is: ", add(20, 50))

After writing the above code (syntaxerror return outside function python), Ones you will print then the output will appear as a “ Total is: 70 ”. Here, line no. 3 is resolved by giving correct indentation of return statement which should be inside the function so, in this way we can solve this syntax error.

You can refer to the below screenshot:

Syntaxerror return outside function python
return outside function python

You may like the following Python tutorials:

This is how to solve python SyntaxError: return outside function error.

Leave a Comment