What does the following program do? Recursive function call - C++ Function

C++ examples for Function:Recursive Function

Description

What does the following program do? Recursive function call

Demo Code

#include <iostream> 
using namespace std; 

int mystery( int, int ); 

int main() /*from   ww  w. j  a v a 2s . c om*/
{ 
   int x = 2, y = 3; 

   cout << "The result is " << mystery( x, y ) << endl; 
}


int mystery( int a, int b ) 
{ 
   if ( b == 1 ) // base case 
       return a; 
   else // recursion step 
       return a + mystery( a, b - 1 ); 
}

Result


Related Tutorials