Java Reference
In-Depth Information
Display 11.3
The Recursive Method
power
(part 2 of 2)
16
if
(n > 0)
17
return
( power(x, n
−
1)*x );
18
else
// n == 0
19
return
(1);
20 }
21 }
Sample Dialogue
3 to the power 0 is 1
3 to the power 1 is 3
3 to the power 2 is 9
3 to the power 3 is 27
Display 11.4
Evaluating the Recursive Method Call
power(2,3)
SEQUENCE OF RECURSIVE CALLS:
1
HOW THE FINAL VALUE IS COMPUTED:
1
power(2, 0)
*2
1 *2
1*2
is
2
power(2, 1)
*2
2 *2
2*2
is
4
power(2, 2)
*2
4 *2
4*2
is
8
power(2, 3)
8
Start Here
power(2,
3)
is
8