 # [Function Practice] Defining variables and the return function? [solved]

I don’t understand why my code didn’t work.
Can anyone help me out?
I tried:

``````def cube(number):
number ** 3
return number

def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return not True
``````

when I click run I get “cube(2) returned 2 instead of 8”

The solution they give is:

``````def cube(number):
return number * number * number

def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return False
``````

`return not True`, why not simple use `False`?

``````number ** 3
``````

you do a math operation, but you do nothing with the result of the math operations, we can prove this:

``````num = 5
num ** 3
print(num) # output: 5
num = num ** 3 # update num variable with result of math operation
print(num) # output: 125
``````
1 Like

I thought when I write return number,
the result of number ** 3 gets stored in number

no, you don’t update the `number` variable. You can of course return the result of the math operation directly (which the solution is doing) without intermediate step.

so after I define a function, then after the : on line 2 I write something to do (ie: number **3 ), and on line 3 I write return number
the result of line 2 does not get stored in the variable following return in line 3

yes, i even demonstrated this with an example:

``````num = 5
num ** 3
print(num) # output: 5
num = num ** 3 # update num variable with result of math operation
print(num) # output: 125
``````

with comments which are hopefully useful and should be of aid to you

I appreciate your time in helping me straighten out my confusion.
I would like to correct my thinking, so I’m asking, does using return number
store the result of the computation that is above return?

no, `return` is what the function is handing back:

``````def cube(number):
number = number ** 3
return number

print cube(3) # use print to print the returned result
x = cube(5) # store the returned result in a variable named x
print x # output: 125
``````

`return` allows us to hand back data at the end of the function.

Thank you. Much appreciated

i didn’t explain `return` at first, because its really easy to find a lot of information (known as documentation) when it comes to things like `return`. Google is your friend

1 Like