30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Procedures 221<br />

40<br />

41 End Function ' Fac<strong>to</strong>rial<br />

42<br />

43 End Class ' FrmFac<strong>to</strong>rial<br />

Fig. Fig. 6.19 6.19 Recursive fac<strong>to</strong>rial program (part 2 of 2).<br />

Common <strong>Program</strong>ming Error 6.8<br />

Forgetting <strong>to</strong> return a value from a recursive procedure can result in logic errors. 6.8<br />

Common <strong>Program</strong>ming Error 6.9<br />

Omitting the base case or writing the recursive step so that it does not converge on the base<br />

case will cause infinite recursion, eventually exhausting memory. This is analogous <strong>to</strong> the<br />

problem of an infinite loop in an iterative (nonrecursive) solution. 6.9<br />

6.15 Example Using Recursion: Fibonacci Series<br />

The Fibonacci series<br />

0, 1, 1, 2, 3, 5, 8, 13, 21, …<br />

begins with 0 and 1 and defines each subsequent Fibonacci number as the sum of the previous<br />

two Fibonacci numbers.<br />

The series occurs in nature and, in particular, describes a form of spiral. The ratio of<br />

successive Fibonacci numbers converges on a constant value near 1.618. This number<br />

occurs repeatedly in nature and has been called the golden ratio or the golden mean.<br />

Humans tend <strong>to</strong> find the golden mean aesthetically pleasing. Architects often design windows,<br />

rooms and buildings so that their ratios of length <strong>to</strong> width are equal <strong>to</strong> the golden<br />

mean. Similarly, postcards often are designed with a golden-mean width-<strong>to</strong>-height ratio.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!