Functions with one default argument
In the previous chapter, you've learned to define functions with more than one parameter and then calling those functions by passing the required number of arguments. In the last video, Hugo built on this idea by showing you how to define functions with default arguments. You will practice that skill in this exercise by writing a function that uses a default argument and then calling the function a couple of times.
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Complete the function header with the function name
shout_echo
. It accepts an argumentword1
and a default argumentecho
with default value1
, in that order. - Use the
*
operator to concatenateecho
copies ofword1
. Assign the result toecho_word
. - Call
shout_echo()
with just the string,"Hey"
. Assign the result tono_echo
. - Call
shout_echo()
with the string"Hey"
and the value5
for the default argument,echo
. Assign the result towith_echo
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define shout_echo
def ____(____, ____):
"""Concatenate echo copies of word1 and three
exclamation marks at the end of the string."""
# Concatenate echo copies of word1 using *: echo_word
echo_word = ____
# Concatenate '!!!' to echo_word: shout_word
shout_word = echo_word + '!!!'
# Return shout_word
return shout_word
# Call shout_echo() with "Hey": no_echo
no_echo = ____
# Call shout_echo() with "Hey" and echo=5: with_echo
with_echo = ____
# Print no_echo and with_echo
print(no_echo)
print(with_echo)