20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Recursive Functions<br />

159<br />

Recursive Functions<br />

The C language supports a capability known as recursive function. Recursive functions<br />

can be effectively used to succ<strong>in</strong>ctly and efficiently solve problems.They are commonly<br />

used <strong>in</strong> applications <strong>in</strong> which the solution to a problem can be expressed <strong>in</strong> terms of<br />

successively apply<strong>in</strong>g the same solution to subsets of the problem. One example might<br />

be <strong>in</strong> the evaluation of expressions conta<strong>in</strong><strong>in</strong>g nested sets of parenthesized expressions.<br />

Other common applications <strong>in</strong>volve the search<strong>in</strong>g and sort<strong>in</strong>g of data structures called<br />

trees and lists.<br />

Recursive functions are most commonly illustrated by an example that calculates the<br />

factorial of a number. Recall that the factorial of a positive <strong>in</strong>teger n,written n!, is simply<br />

the product of the successive <strong>in</strong>tegers 1 through n.The factorial of 0 is a special case and<br />

is def<strong>in</strong>ed equal to 1. So 5! is calculated as follows:<br />

5! = 5 x 4 x 3 x 2 x 1<br />

= 120<br />

and<br />

6! = 6 x 5 x 4 x 3 x 2 x 1<br />

= 720<br />

Compar<strong>in</strong>g the calculation of 6! to the calculation of 5!, observe that the former is equal<br />

to 6 times the latter; that is, 6! = 6 x 5!. In the general case, the factorial of any positive<br />

<strong>in</strong>teger n greater than zero is equal to n multiplied by the factorial of n - 1:<br />

n! = n x (n - 1)!<br />

The expression of the value of n! <strong>in</strong> terms of the value of (n-1)! is called a recursive def<strong>in</strong>ition<br />

because the def<strong>in</strong>ition of the value of a factorial is based on the value of another<br />

factorial. In fact, you can develop a function that calculates the factorial of an <strong>in</strong>teger n<br />

accord<strong>in</strong>g to this recursive def<strong>in</strong>ition. Such a function is illustrated <strong>in</strong> Program 8.16.<br />

Program 8.16 Calculat<strong>in</strong>g Factorials Recursively<br />

#<strong>in</strong>clude <br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

unsigned <strong>in</strong>t j;<br />

unsigned long <strong>in</strong>t factorial (unsigned <strong>in</strong>t n);<br />

for ( j = 0; j < 11; ++j )<br />

pr<strong>in</strong>tf ("%2u! = %lu\n", j, factorial (j));<br />

}<br />

return 0;<br />

// Recursive function to calculate the factorial of a positive <strong>in</strong>teger

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

Saved successfully!

Ooh no, something went wrong!