Home Segments Top Top Previous Next

443: Mainline

Now you are ready to perform the recursion trick: You replace the call to powerOf2 in recursivePowerOf2 by a call to recursivePowerOf2 itself:

public static int recursivePowerOf2 (int n) {
 if (n == 0) {return 1;}
 else {
  return 2 * recursivePowerOf2(n - 1); 
 } 
} 

The new version works for two reasons: