Home Segments Index Top Previous Next

343: Mainline

Now for the recursion trick: You replace power_of_2 in recursive_power_of_2 by recursive_power_of_2 itself:

int recursive_power_of_2 (int n) { 
  if (n == 0) 
     return 1; 
    else return 2 * power_of_2 (n - 1); 
} 
  
int recursive_power_of_2 (int n) {   
  if (n == 0)  
     return 1;  
    else return 2 * recursive_power_of_2 (n - 1); 
} 

The new version works for two reasons: