I have got this example of a recursive function:
<code>
function power(base, exponent) {
if (exponent == 0) {
return 1;
}
else
{
return base * power(base, exponent - 1);
}
}
alert(power(2, 10));
</code>
I would have thought that the result (which equates to 1024) would need to be stored in a variable inside the function and that value returned for the alert to use.
As it is, the function returns the calculation with return 1 (return true also works).
I was hoping someone could explain how this value is stored throughout the function and how "return true" is able to provide the caller (alert), with this value.
<code>
function power(base, exponent) {
if (exponent == 0) {
return 1;
}
else
{
return base * power(base, exponent - 1);
}
}
alert(power(2, 10));
</code>
I would have thought that the result (which equates to 1024) would need to be stored in a variable inside the function and that value returned for the alert to use.
As it is, the function returns the calculation with return 1 (return true also works).
I was hoping someone could explain how this value is stored throughout the function and how "return true" is able to provide the caller (alert), with this value.