Functions: #6 Practice Makes Perfect


I don't understand why...

def cube(n):
    n = n ** 3
    return n

works, and...

def cube(n):
    return n ** 3

Also works, but...

def cube(n):
    return n = n ** 3

does not work. Can anyone explain why this third method does not work?


return does not work with a statement, only an expression (or nothing). That is why we refer to it as the return value.

n = n ** 3

is a statement.