I want to understand recursion

this lesson is strange for me I am, trying to understand it step by step.
what does this means
However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] . That means you can rewrite multiply in terms of itself and never need to use a loop.

replace loops

It is very, very, very common to be very, very, very confused about recursion, especially when learning. I would suggest searching the forum for many responses on the subject and hen ask if you have more questions. I know that I have answered that exact question recently. I’m on a tablet atm, but let me see if I can find that…

2 Likes

Sometimes it helps to use real numbers. The multiply function multiplies the first n numbers of an array. So if we had the array [2, 3, 4] and you wanted to multiply the first three numbers then you would call the function as:

multiply([2, 3, 4], 3);

If you think about it, this function is the equivalent of

2 * 3 * 4

So for any three numbers we wanted to multiply together we could just put them in an array and call this function. But we can also do the same with two numbers. So if we just wanted to multiply the first two numbers we could do:

multiply([2, 3, 4], 2)

Which is the same as

2 * 3

So now we can substitute this into the original one:

multiply([2, 3, 4], 2) * 4

to get back to the original problem.

So multiply([2, 3, 4], 3) is equal to multiply([2, 3, 4], 2) * 4.

2 Likes

It was also discussed here: Recursion n-1 checking understanding

Hello @Hamza-Noah,

trying to understand it step by step.

You can use python tutor to see step by step how the function is executed:

https://pythontutor.com/live.html#mode=edit

Not the same exercise but similar (the video is a demo of how to use python tutor):

I wrote a blog post explaining the exercise using python tutor (it has 27 images):

https://diegoperezm.github.io/blog/recursion.html

I recorded a video (Spanish) explaining the exercise step by step:

Cheers and happy coding :slight_smile: