Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

SyntaxError: 'Return' Outside Function in Python

|| 0

SyntaxError: 'Return' Outside Function in Python
Stechies

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 defined function.

Example:

# Python 3 Code

def myfunction(a, b):
  # Print the value of a+b
  add = a + b

return(add)

# Print values in list
print('Addition: ', myfunction(10, 34));

Output:

File "t.py", line 7
    return(add)
    ^
SyntaxError: 'return' outside function

As you can see that line no. 7 is not indent 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:

# Python 3 Code

def myfunction(a, b):
  # Print the value of a+b
  add = a + b

  return(add)

# Print values in list
print('Addition: ', myfunction(10, 34));

Output:

Addition:  44

 


Advertisement
Advertisement