FAQ: Introduction to Functions - Review

This community-built FAQ covers the “Review” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

12 posts were split to a new topic: Why does it print 10 times?

11 posts were split to a new topic: Questions about the specifics of strings

A post was split to a new topic: Why is this variable equal to a function?

Can someone take me through this program step by step?

1. def repeat_stuff(stuff, num_repeats=10):
2. return stuff*num_repeats

3. lyrics = repeat_stuff("Row ", 3) + "Your Boat. "
4. song = repeat_stuff(lyrics)

5. print(song)

-I understand that in line 1 we are defining a function with 2 arguments.

  • Line 2 we are returning the value so that we can use the arguments outside the function (maybe in it too?).
    -Line 3 is where you lose me. You are calling the function inside a variable using its arguments?
    -Line 4 I am lost. Youre calling the function again within a variable but instead of an argument you are calling the previous variable? Why do you not need 2 arguments since the functioned is defined with 2 to begin with?
1 Like

i can highly recommend a tool to walk through your program like a debugger or:

Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java

we return a string. so for example:

repeat_stuff("Row ", 3)

then the function would return "Row Row Row".

we call the function with two arguments: "Row", which is a string, and 3 which is a integer.

the result of this function call is "Row Row Row" as we just determined. We then join/concat "Row Row Row" with "Your Boat"

so now we completed one lyric/line of our song.

variables are valid arguments, lyrics variable holds a string.

the second parameter has a default value (10), so when we don’t provide a second argument, the default is used.

so now we have a song which consist of 10 lines.

How do I know when to use + or comma to concatenate?
Does ‘cannot concatenate an integer’ apply to situations where either + or a comma is used?

Thanks.

Concatenation is a string and list operation, and it uses exclusively the + operator. The comma has only one purpose, to separate data.

When we print a comma separated list (a sequence) the commas don’t have any special meaning.

print ('one', 'two', 'three', 'four')

# one two three four

That is not concatenation. This is,

"The meaning of life is, " + 42 + "."

but as you’ve discovered this will raise an error since integers cannot be concatenated with strings unless they are first cast to a str object.

"The meaning of life is, " + str(42) + "."

Can someone explain this coding step by step? I am confused with the 10, 3, repeat_stuff(song) :disappointed_relieved:

There are explanations in this topic? Did you read them? Including a tool/website where you can run through your code step by step.

Why are you confused? If we understand the confusion, we can help better

I’m having trouble with step 3 for this one.
I don’t understand how to return the function.
I am doing this: return stuff*num_repeats
it just won’t work for me

We will need to see your code. Please post in a reply.

1 Like
def repeat_stuff(stuff, num_repeats=10):
  return stuff*num_repeats

lyrics = repeat_stuff("Row ", 3) + "Your Boat."
print(lyrics)

What i don’t get here is why if i’ve setted the default value of repeats_num to 10, then if i’d call the function with parameters ("Row ", 3) i’d have a printed output of only 3 Row (Row, Row, Row) and not 10 Row, or 10*3 Row.
I Mean, what’s the use to specify the default value of an argument then? Why it isn’t take in consideration when i call the function?

1 Like

for when no argument is supplied:

repeat_stuff("Row ") + "Your Boat."

now you will get 10*“row”.

The great thing about defaults are that they can be overwritten when the need arises.

But then, in the next step, if i create a variable song = repeat_stuff(lyrics)
Where the parameters where specified to be ("Row ", 3), i actually have ten times the string “Row Row Row Your Boat”.
So can i assume that default value of a parameter in a function still is in count somehow?
Can you explain to me this, with some other words please?
I Mean, i saw how it works, but i must say that it also has created a little bug in my understanding

here:

song = repeat_stuff(lyrics)

You make a separate function call, given there is no argument for num_repeats so the default is used (10)

so first we create one line of of our song:

lyrics = repeat_stuff("Row ", 3) + "Your Boat."

so now lyrics is:

"Row Row Row your boat"

which we are now going to repeat 10 times to make a song:

song = repeat_stuff(lyrics)

you now have a song :slight_smile:

1 Like

Hi guys

stuck on step 5 of this exercise. It says expected lyrics to be defined. Please help

You do exactly what it says

5.Add repeat_stuff("Row ", 3) and the string "Your Boat. " together and save the result to a variable called lyrics.

lyrics = repeat_stuff("Row ", 3) + "Your Boat. "

Thanks well, its more of the little things that one has to remember. I had lyrics indented which gave me and error " Expected lyrics to be defined", Sorted thanks

The solution you are posting is what makes no sense. It clearly does not follow the rather explicit instructions of what to actually do, for this step…

Define a function called repeat_stuff that takes in two inputs, stuff , and num_repeats .
We will want to make this function print a string with stuff repeated num_repeats amount of times. For now, only put an empty print statement inside the function.

def repeat_stuff(stuff, num_repeats):
    print ()

THAT is all we are directed to do. As to the final question above, that is rather moot, in light of things.