Functions 6/19 Question


#1

https://www.codecademy.com/courses/learn-python/lessons/functions/exercises/practice-makes-perfect?action=lesson_resume

First, def a function called cube that takes an argument called number. Don’t forget the parentheses and the colon!

Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).

Define a second function called by_three that takes an argument called number.

if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.

My code is here:

def cube(number):
  return number**3

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

However, the correct code is this:

def cube(number):
  return number**3

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

I have a different understanding with “by_three should call cube(number) and return its result” reflected in my code above. Please help! Thanks.


#2

if the instructions where:

define a function named example and make it return 3, you would do:

def example():
     return 3

right?

same here, the by_three function needs to return cube(number).

In other words, in the body of by_three function, the result of calling cube with argument number should be returned. There is no where mentioned that by_three should call itself (which would cause recursion)


#3

Thanks. But why would we define a function and then return something that has nothing to do with the function defined? I thought we would use the function, for example, to call it on some variables when returning. (I just started to learn programming if it’s a dumb question.)


#4

A function allows us to repeat a task, so we could for multiply numbers determine if its divisible by three:

# function calls
by_three(5)
by_three(6)
by_three(7)
by_three(8)
by_three(9)

then the function determines if the number is divisible by 3, the function returns the cube of the number else the function returns false

why would have the function to return something that has to do with the function defined?


#5

That makes sense. Thanks!


#6

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