Does one of the input values have to share the same data type as the return value?

Question

In a Python function, does at least one of the input values have to share the same data type as the return value?

Answer

No, none of the parameter values must match the return value’s type. Parameters and return values of a function have no real intrinsic connection. The connection, if any, is made by us when writing the code, and deciding what the code itself does inside the function, which can have the return type be the same as the input value type. However, neither will be constrained to the other’s data type, unless we implemented it that way.

Example

# The input values are numbers,
# while the return value is a string.
def abc_repeater(a_repeats=0, b_repeats=0, c_repeats=0):
  return "a" * a_repeats + "b" * b_repeats + "c" * c_repeats

# This will output “aaaaabbbbbccccc”
print(abcRepeater(5, 5, 5)) 

# The input value must be a number for this function.
# This is because of how the code was written, 
# rather than a strict rule.
def add_ten(num):
  return num + 10
10 Likes

i dont understand please

The answer to the question,

… is “No.”

This example:

# The input values are numbers,
# while the return value is a string.
def abc_repeater(a_repeats=0, b_repeats=0, c_repeats=0):
  return "a" * a_repeats + "b" * b_repeats + "c" * c_repeats

# This will output “aaaaabbbbbccccc”
print(abcRepeater(5, 5, 5)) 

… showed a function whose input values were numbers, and which returned a string.

4 Likes

I got it now. Thanks alot.

1 Like

can someone explain this whole program

https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/review?action=resume_content_item

wouldn’t you have to type in str( ) for a_repeats since that variable is a string and “a” is a string?

No, in the example:

The function returns the string “a” a_repeats times. For example if a_repeats is set to 5 (like in the example) the function will return “a” 5 times (aaaaa). The same goes for all the other letters in the example. Hope that helps.

Please follow along on your own page. :wink:
For the first step we are told to define a function called repeat_stuff with 2 inputs, stuff , and num_repeats as well as put an empty print() statement inside our function definition. We get the following piece of code.

#function definition
**def repeat_stuff(stuff, num_repeats):**
**  print()**

We then call our function with the value "Row " for stuff and 3 for num_repeats, below our function definition (right now our function won’t do anything so don’t worry).

#function definition
def repeat_stuff(stuff, num_repeats):
  print()
#function call
**repeat_stuff("Row ", 3)**

Then we add functionality to our function by replacing our useless print() statement with a return statement that returns stuff*num_repeats or stuff 'num_repeats" times.

#function definition
def repeat_stuff(stuff, num_repeats):
  **return stuff*num_repeats**
#function call
repeat_stuff("Row ", 3)
#returns "Row Row Row" the same as if we wrote "Row "+ "Row "+"Row ".
#                 1         2       3                                                     1              2            3

Now set the default value of input `num_repeats’ to 10

#function definition
def repeat_stuff(stuff, **num_repeats=10**):
  return stuff*num_repeats
#function call
repeat_stuff("Row ", 3)
#returns "Row Row Row" the same as if we wrote "Row "+ "Row "+"Row ".
#                 1         2       3                                                     1              2            3

Now we create a variable lyrics and assign it a value of repeat_stuff("Row ", 3) + "Your Boat. "

#function definition
def repeat_stuff(stuff, num_repeats=10):
  return stuff*num_repeats
#function call
**lyrics =** repeat_stuff("Row ", 3) **+ "Your Boat. "**
#returns "Row Row Row" the same as if we wrote "Row "+ "Row "+"Row ".
#                 1         2       3                                                     1              2            3

Now we take lyrics which contains the value "Row Row Row Your Boat. ", put it into our function with no second argument, and store the returned value in a variable song. Since we set the default value of num_repeats to 10 our function will return "Row Row Row Your Boat. " 10 times.

#function definition
def repeat_stuff(stuff, num_repeats=10):
  return stuff*num_repeats
#function call
lyrics = repeat_stuff("Row ", 3) + "Your Boat. "
#returns "Row Row Row" the same as if we wrote "Row "+ "Row "+"Row ".
#                 1         2       3                                                     1              2            3
**song = repeat_stuff(lyrics)**

Then for the last step print() song to the console.

#function definition
def repeat_stuff(stuff, num_repeats=10):
  return stuff*num_repeats
#function call
lyrics = repeat_stuff("Row ", 3) + "Your Boat. "
#returns "Row Row Row" the same as if we wrote "Row "+ "Row "+"Row ".
#                 1         2       3                                                     1              2            3
song = repeat_stuff(lyrics)
**print(song)**

Hope this helps. Thanks for letting me “teach” you. Teaching is the best way to mastery. :grin:
P.S. if you’re wondering what the point of this exercise is, it’s just for practice.