You can modify the code to print a trace of the recursion. That can help you understand what is going on.
/** * Print string form of `o`, but indented with n spaces */ private static void printIndented(int n, Object o) { while (n-->0) System.out.print(" "); System.out.println(o); } /* * Added a third param `d` to keep track of the depth of the recursion */ public static double power1(double a, int n, int d) { // Entering a "possible" recursive call printIndented(d, "call power1, a=" + a + ", n=" + n + ", d=" + d); double result; if(n == 0) { // Returning from the base case, this should have the largest depth. printIndented(d, "return 1.0"); return 1; } else { result = power1(a, n - 1, d + 1); // Return from intermediate recursive calls, we print // the value of power1(a, n-1) as well. printIndented(d, "return " + result + " * " + a); return result * a; } } public static void main(String [] args) { System.out.println(power1(1.4, 3, 0)); }
Output
call power1, a=1.4, n=3, d=0 call power1, a=1.4, n=2, d=1 call power1, a=1.4, n=1, d=2 call power1, a=1.4, n=0, d=3 return 1.0 return 1.0 * 1.4 return 1.4 * 1.4 return 1.9599999999999997 * 1.4 2.7439999999999993
As you can see, the value from the inner return becomes the result in the outer return statement.
double (from the recursive call) * double a, so that mean you get a double back, which is exactly as the method power1 defines.