Hello everyone here !
I am new to this forum and I have a beginner's question about a simple recursive (Call by ref) function : x and y are int and x times y is y + y + y +.... x times. Here is my function :
int mult(int x, int y, int &prd) {
if (x == 0) // case: x = 0 product is 0
prd = 0;
else
prd = y + mult(x - 1, y, prd);
return prd;
}
My question is : when x decreases to 0 the return value of prd should be 0 but instead I (happily) get x times y product. I don't understand.
Thanks for reading.
Alain
Well passing prd around as a reference, as well as returning the same as a value makes no sense.
One thing is almost certainly overwriting the other in some unexpected way.
Also,
1 2 3
if ( x == 0 ) prd = 0;
elseif ( x == 1 ) prd = y;
else prd = y + mult(x - 1, y, prd);