Here is the example of recursion I struggle to understand.
So, here we have
However, notice that
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1].
Mine issue is that I can’t understand this expression and how can I notice the thing.
A bit of my brainstorming:
So, we have 2 parts of the equation:
multiply(arr, n)- the function that multiplies elements of the array
multiply(arr, n - 1) * arr[n - 1]- since that moment I stop understanding things. As I can understand this expression, we multiply the result of the function and a certain element of the array. But why do we use n-1 instead of n as a second argument?
multiply(arr, n - 1)does stand for? As far as I understand, it’s the value of previous operations, isn’t it? And the only reason we have minus one in the second argument
n-1is just that it calculates everything that happened before. Am I right?
arr[n-1]- here we access an element of an array. But why do we have to write here
n-1instead of just
- Finally, I can’t understand equality. Why the result of the function with one argument can be equal to the result of multiplying the same function but with the previous value of an argument by the value of the previous element of an array.
I hope you can help me with that. Thank you.