SyntaxError: 'return' outside function

SyntaxError: ‘return’ outside function

In python SyntaxError: ‘return’ outside function we face this error message when we create a def function and create a return special statement and use an outside function or do incorrect indentation then the python interpreter shows this error message, If you want to fix this error you need to just use return inside a def function with add 4 spaces or just use tab key one time, For more clarification see the below example.

Wrong Code

def welcome(name):
    return
    

return "Welcome, " + name + "!"

user_name = input("What is your name? ")
print(welcome(user_name))

Error Massage

  File "/home/kali/python/webproject/error/main.py", line 5
    return "Welcome, " + name + "!"
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'return' outside function

Wrong code line

return "Welcome, " + name + "!"

Correct code line

def welcome(name):
    return "Welcome, " + name + "!"
  • Wrong action: Outside or incorrect indentation.
  • Correct action: In-site with 4 spaces.

Entire Correct Code line

def welcome(name):
    return "Welcome, " + name + "!"

user_name = input("What is your name? ")
print(welcome(user_name))

What is SyntaxError: ‘return’ outside function?

In python we face this error message when we create a def function and create a return special statement and use an outside function or do an incorrect indentation then the python interpreter shows this error message.

How to fix SyntaxError: ‘return’ outside function?

If you want to fix this error you need to just use return inside a def function with add 4 spaces or just use the tab key one time, For more clarification see the above example.

For more information Visit YouTube Channel.

Scroll to Top
Top 7 Gaming Project In python Save ₹25,000 Laptop under ₹11,999 Top 7 Funny error in Python Unicodeescape Most dangerous error in Python Hero Electric Battery Price In 2023