# 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?

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.
return num + 10
``````
10 Likes

… 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.

6 Likes

I got it now. Thanks alot.

2 Likes

can someone explain this whole program

2 Likes

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.

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.
P.S. if you’re wondering what the point of this exercise is, it’s just for practice.

5 Likes

Part 3 of this was very confusing, said delete print and add return without specifying the values and then said it should return x,y,z which didn’t make a lot of sense.

So part 3 essentially calls for replacing the ` print()` with the return specified here:

`return stuff * num_repeats`

You’re basically returning the computation of the two arguments `(stuff, num_repeats)` to the function `repeat_stuff` so that you can use it later for the variables ` lyrics` and `song`.

Hope that makes sense!

can You please explain me this specific line ?

In this exercise we are able to simply write `song = repeat_stuff(lyrics)` to assign the variable `song` to the variable `lyrics` repeated 10 times instead of having to specify how many times we want `lyrics` to be repeated since we set a default value for our `num_repeats` argument to 10

``````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 ".
#
``````

When we set a default value for an argument our function acts like we input our default value when we don’t input any value for that argument. If we want to have our argument `stuff` repeated any different amount of times than 10 we have to specify how many times we would like it repeated. `repeat_stuff("Row ", 3)`
Hope that helps.

what I’m having trouble with this line is `repeat_stuff(lyrics)`
what are we doing actually here ? what is the logic behind putting `lyrics` inside `repeat_stuff()` function ?

You are calling the function `repeat_stuff` with the argument `lyrics`. Does it make more sense with `repeat_stuff(stuff=lyrics)`? The argument `lyrics` is assigned to the parameter (name inside the function) `stuff` and the function executes it’s code block.

1 Like

ok. That means once I’ve assigned values to the functions parameters and assigned itself to another variable, I can pass that variable into that same function call ?

Yes a function is a reusable piece of code. We assigned the return value of `repeat_stuff("Row", 3)` concatenated with `"Your Boat."` . Later when we call our function with different arguments we’ll get a different return value. Better Explanation of Functions

THANK YOU!!! There’s a lot of vocabulary I need to become familiar with.

Well written and executed. Straight to the point. Thank you very much for this