This is the function that multiplies a and b:
0 int mult(int a, int b){
1 if(a==0){
2 return 0;
3 } else{
4 a=a-1;
5 int c = mult(a,b);
6 int d = b + c;
7 return d;
8 }
9 }
I am playing with arguments 2 and 3: the result is 6, but why?
In line 5 I will get 0 after the second a=a-1; and then d is 3 then return 3 and not 6. Am I stupid or is it also confusing for you?
Let’s go step by step [ mult(2, 3) ]: –
NOTE: –
States of recursive calls are stored on stack. So, as the base condition is reached, the stack starts unwinding, passing the result of the current call to the previous call, thus reaching the state where it started with a final result.