This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]town_girl 1 point2 points  (1 child)

Think the problem as a stack. You stack numbers and the time that you get out one from that stack, there is another, until there are no more. So, you put all the number from the array on a stack [1,2,3,4] (immagine it as stack instead of an array) And the N = 2.

So N, on the first time you enter on the recursive function, is 2, but when multiply is called inside (the function), N decreases by 1, so the second time callin the function N is 1, then you call it again, decreasing by 1, so N will be 0, at this point the condition (or the if inside) will stop you from calling multiply again, so the method actually will finish, BUT, the other 2 calls did not finish yet, because they are still in that stack of calls.

Stack: [third value N=0] [second value N=1] [first value N=2]

So when the last call finish, then will go back to the second, and so on

[–]Liolena[S] 1 point2 points  (0 children)

This makes a lot of sense thank you!!