Now let s look at an example that involves a recursive call Consider the expression

Data Matrix Drawer In C#Using Barcode drawer for VS .NET Control to generate, create ECC200 image in VS .NET applications.

power(2, 1)

ECC200 Encoder In VS .NETUsing Barcode generator for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.

When the function is called, the value of x is set equal to 2, the value of n is set equal to 1, and the code in the body of the function definition is executed Since this value of n is greater than 0, the following return statement is used to determine the value returned:

Print DataMatrix In .NETUsing Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.

return ( power(x, n - 1)*x );

Paint Data Matrix In Visual Basic .NETUsing Barcode printer for Visual Studio .NET Control to generate, create DataMatrix image in .NET framework applications.

which in this case is equivalent to

UCC - 12 Printer In VS .NETUsing Barcode creation for ASP.NET Control to generate, create UPC-A Supplement 5 image in ASP.NET applications.

return ( power(2, 0)*2 );

USS Code 128 Encoder In C#Using Barcode generator for Visual Studio .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.

At this point the computation of power(2, 1) is suspended, a copy of this suspended computation is placed on the stack, and the computer then starts a new function call to compute the value of power(2, 0) As you have already seen, the value of power(2, 0) is 1 After determining the value of power(2, 0), the computer replaces the expression power(2, 0) with its value of 1 and

Paint Code 128 Code Set A In JavaUsing Barcode creator for Java Control to generate, create Code 128C image in Java applications.

Recursion

Generating Barcode In VS .NETUsing Barcode drawer for VS .NET Control to generate, create bar code image in .NET applications.

resumes the suspended computation The resumed computation determines the final value for power(2, 1) from the above return statement as follows:

Bar Code Generation In Visual Basic .NETUsing Barcode creator for .NET Control to generate, create bar code image in .NET framework applications.

power(2, 0)*2

DataMatrix Generation In .NET FrameworkUsing Barcode creation for Visual Studio .NET Control to generate, create ECC200 image in .NET framework applications.

is 1*2, which is 2

Bar Code Encoder In VS .NETUsing Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications.

Thus, the final value returned for power(2, 1) is 2 So, the following would set the value of result4 equal to 2:

Code 3/9 Generator In Visual C#Using Barcode printer for VS .NET Control to generate, create Code-39 image in .NET framework applications.

int result4 = power(2, 1);

Painting Code39 In Visual Basic .NETUsing Barcode encoder for .NET Control to generate, create Code-39 image in .NET applications.

Larger numbers for the second argument will produce longer chains of recursive calls For example, consider the statement

Draw Code 39 Full ASCII In JavaUsing Barcode creator for Java Control to generate, create Code 3/9 image in Java applications.

cout << power(2, 3);

Print Barcode In C#Using Barcode creator for .NET Control to generate, create bar code image in Visual Studio .NET applications.

The value of power(2, 3) is calculated as follows:

Generating Barcode In JavaUsing Barcode encoder for Java Control to generate, create bar code image in Java applications.

power(2, 3) is power(2, 2)*2 power(2, 2) is power(2, 1)*2 power(2, 1) is power(2, 0)*2 power(2, 0) is 1 (stopping case)

Bar Code Encoder In VS .NETUsing Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.

When the computer reaches the stopping case power(2, 0), there are three suspended computations After calculating the value returned for the stopping case, it resumes the most recently suspended computations to determine the value of power(2, 1) After that, the computer completes each of the other suspended computations, using each value computed as a value to plug into another suspended computation, until it reaches and completes the computation for the original call, power(2, 3) The details of the entire computation are illustrated in Display 134

GTIN - 13 Scanner In Visual Studio .NETUsing Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.

Self-Test Exercises

Code 128B Generator In Visual Basic .NETUsing Barcode generator for VS .NET Control to generate, create Code 128 Code Set C image in .NET applications.

12 What is the output of the following program

Generating EAN13 In Visual Studio .NETUsing Barcode generation for Visual Studio .NET Control to generate, create EAN13 image in VS .NET applications.

#include <iostream> using std::cout; using std::endl; int mystery(int n); //Precondition n >= 1 int main( ) { cout << mystery(3) << endl; return 0; } int mystery(int n) {

Recursive Functions That Return a Value

Display 134 Evaluating the Recursive Function Call power(2,3)

SEQUENCE OF RECURSIVE CALLS

HOW THE FINAL VALUE IS COMPUTED

power(2, 0) *2

is 2

2 *2

power(2, 1) *2

is 4

4 *2

power(2, 2) *2

4*2 power(2, 3)

is 8

Start Here

power(2, 3)

is 8

if (n <= 1) return 1; else return ( mystery(n - 1) + n ); }

13 What is the output of the following program What well-known mathematical function is rose

#include <iostream> using std::cout; using std::endl; int rose(int n); //Precondition: n >= 0 int main( ) { cout << rose(4) << endl;

Recursion

return 0; } int rose(int n) { if (n <= 0) return 1; else return ( rose(n - 1) * n ); }

14 Redefine the function power so that it also works for negative exponents In order to do this you will also have to change the type of the value returned to double The function declaration and header comment for the redefined version of power are as follows:

double power(int x, int n); //Precondition: If n < 0, then x is not 0 //Returns x to the power n

Hint: x n is equal to 1/( x n )

Thinking Recursively

There are two kinds of people in the world, those who divide the world into two kinds of people and those who do not

Anonymous

s RECURSIVE DESIGN TECHNIQUES

When de ning and using recursive functions, you do not want to be continually aware of the stack and the suspended computations The power of recursion comes from the fact that you can ignore that detail and let the computer do the bookkeeping for you Consider the example of the function power in Display 133 The way to think of the de nition of power is as follows:

power(x, n)

returns

power(x, n - 1)*x

Since xn is equal to xn-1*x, this is the correct value to return, provided that the computation will always reach a stopping case and will correctly compute the stopping case So, after checking that the recursive part of the de nition is correct, all you need check