Programming in C++ l/ Recursive definition of power function float Power(/*in*/float x,/in"/ int n) l/ Precondition: x=0 & Assigned(n) lI Postcondition: Function value == x raised to the power n if(n==0) ∥ base case return 1 else if(n>0) ∥ first general/case return( x* Power(x, n-1)); else ∥ second genera/case return(1.0/Power(x,-n)); 16
16 // Recursive definition of power function float Power ( /* in */ float x, /* in */ int n ) // Precondition: x != 0 && Assigned(n) // Postcondition: Function value == x raised to the power n. { if ( n == 0 ) // base case return 1; else if ( n > 0 ) // first general case return ( x * Power ( x , n - 1 ) ) ; else // second general case return ( 1.0 / Power ( x , - n ) ) ; }
Programming in C++ At Times Base Case Can Be Do Nothing void Printstars(/* in /int n) l Prints n asterisks, one to a line ∥ Precondition: n is assigned ∥ Postcondition: IFn>0, n stars have been printed, one to a line ELSE no action has taken place f(n<=0) ∥ base case ∥ Do nothing se ∥ genera/case cout <<*<<endl Printstars(n-1); ∥ CAN REWRITE AS
17 void PrintStars ( /* in */ int n ) // Prints n asterisks, one to a line // Precondition: n is assigned // Postcondition: // IF n > 0, n stars have been printed, one to a line // ELSE no action has taken place { if ( n <= 0 ) // base case // Do nothing else // general case { cout << ‘*’ << endl ; PrintStars ( n - 1 ) ; } } At Times Base Case Can Be: Do Nothing // CAN REWRITE AS . .
Programming in C++ Recursive Void Function void Printstars(/ in / int n l Prints n asterisks, one to a line // Precondition: n is assigned ∥ Postcondition: IFn>0, n stars have been printed, one to a line ELSE no action has taken place f(n>0) ∥ general case cout<<‘*<<endl; Printstars(n-1); /base case is empty else-clause
18 Recursive Void Function void PrintStars ( /* in */ int n ) // Prints n asterisks, one to a line // Precondition: n is assigned // Postcondition: // IF n > 0, n stars have been printed, one to a line // ELSE no action has taken place { if ( n > 0 ) // general case { cout << ‘*’ << endl ; PrintStars ( n - 1 ) ; } // base case is empty else-clause }
Programming in C++ PrintStars(3) Trace of Call a1: Print Stars(3) is printed Call 2. Print Stars(2) 2 is printed Call 3. Print Stars(1) is printed Call 4. Print Stars(0) 0 19 Do nothing
19 Call 1: PrintStars(3) * is printed Call 2: PrintStars(2) * is printed Call 3: PrintStars(1) * is printed Call 4: PrintStars(0) Do nothing PrintStars(3) Trace of Call n 3 n 2 n 1 n 0
Programming in C++ Recursive Mystery Function int Find(/ in int b,/ in / int a l Simulates a familiar integer operator // Precondition: a is assigned & a>0 & b is assigned & b>=0 ∥ Postcondition ∥ Function value == 777 if(b<a) ∥ base case return o else /general case return(1+ Find(b-a,a)); 20
20 Recursive Mystery Function int Find( /* in */ int b, /* in */ int a ) // Simulates a familiar integer operator // Precondition: a is assigned && a > 0 // && b is assigned && b >= 0 // Postcondition: // Function value == ??? { if ( b < a ) // base case return 0 ; else // general case return ( 1 + Find ( b - a , a ) ) ; }