Home Segments Index Top Previous Next

347: Mainline

You can experiment with recursive_power_of_2 in a program such as this:

#include  
// Define recursive_power_of_2 function: 
int recursive_power_of_2 (int n) { 
  if (n == 0) 
    return 1; 
  else 
    return 2 * recursive_power_of_2 (n - 1); 
} 
// Test recursive_power_of_2 function in main: 
main ( ) { 
  cout << "2 to the 0th power is " << recursive_power_of_2 (0) << endl 
       << "2 to the 1st power is " << recursive_power_of_2 (1) << endl 
       << "2 to the 2nd power is " << recursive_power_of_2 (2) << endl 
       << "2 to the 3rd power is " << recursive_power_of_2 (3) << endl; 
} 
--- Result --- 
2 to the 0th power is 1 
2 to the 1st power is 2 
2 to the 2nd power is 4 
2 to the 3rd power is 8