synthaxError: 'return' outside function


In the Hint section it says I should use return instead of print but then I got the following problem.
"syntaxError: 'return' outside function.
My question is what does it mean when I get this error, I tried to import it as I was thought that earlier in the Section. Help would be appreciated.

from python import *
def shut_down(s):
    return s
if shut_down() == yes :
    return s("Shutting down")
elif shut_down() == no :
    return ("Shutdown aborted")
    return ("Sorry")


Reveiw functions

return is a keyword which is standard included, you don't have to import it.

How to say this nicely? This code is a bit of a mess. look at this simple function:

def example(s):
   print s
example("how parameters work")

s is the function parameter, it will hold the value you pass when calling the function (in this case the string: how parameters work)

so you need to compare s with "yes" and "no"

also, a function ends the moment a return keyword is reached.

one more thing, you can nest an if condition inside a function:

def example():
   if True:
      print "nested if condition inside function"

your if condition is outside the function, which means the return keyword is outside the functions, return keyword can only be used inside functions


return is used to terminate the execution of the function and to return value. Let's say that we have a function square, now we can do something like this:

x = square(2) + square(4)

and after executing the square function value of x will be determined as:

x = 4 + 16 = 20

This means that you can use return only inside functions. Indentation levels are important:

def square(n):
	return n * n # <- inside function
x = square(2) + square(4) # <- outside function

You don't have to import return :slight_smile:


Thank you for the reply very helpful.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.