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.

126 Chapter 8 Work<strong>in</strong>g with Functions<br />

Program 8.5<br />

Output<br />

The gcd of 150 and 35 is 5<br />

The gcd of 1026 and 405 is 27<br />

The gcd of 83 and 240 is 1<br />

The function gcd is def<strong>in</strong>ed to take two <strong>in</strong>teger arguments.The function refers to these<br />

arguments through their formal parameter names u and v. After declar<strong>in</strong>g the variable<br />

temp to be of type <strong>in</strong>t, the program displays the values of the arguments u and v,<br />

together with an appropriate message at the term<strong>in</strong>al.The function then calculates and<br />

displays the greatest common divisor of the two <strong>in</strong>tegers.<br />

You might be wonder<strong>in</strong>g why there are two pr<strong>in</strong>tf statements <strong>in</strong>side the function<br />

gcd.You must display the values of u and v before you enter the while loop because their<br />

values are changed <strong>in</strong>side the loop. If you wait until after the loop has f<strong>in</strong>ished, the values<br />

displayed for u and v do not at all resemble the orig<strong>in</strong>al values that were passed to the<br />

rout<strong>in</strong>e. Another solution to this problem is to assign the values of u and v to two variables<br />

before enter<strong>in</strong>g the while loop.The values of these two variables can then be displayed<br />

together with the value of u (the greatest common divisor) us<strong>in</strong>g a s<strong>in</strong>gle pr<strong>in</strong>tf<br />

statement after the while loop has completed.<br />

Return<strong>in</strong>g Function Results<br />

The functions <strong>in</strong> Program 8.4 and 8.5 perform some straightforward calculations and<br />

then display the results of the calculations at the term<strong>in</strong>al. However, you might not<br />

always want to have the results of your calculations displayed.The C language provides<br />

you with a convenient mechanism whereby the results of a function can be returned to<br />

the call<strong>in</strong>g rout<strong>in</strong>e.This is not new to you because you’ve used it <strong>in</strong> all previous programs<br />

to return from ma<strong>in</strong>.The general syntax of this construct is straightforward<br />

enough:<br />

return expression;<br />

This statement <strong>in</strong>dicates that the function is to return the value of expression to the<br />

call<strong>in</strong>g rout<strong>in</strong>e. Parentheses are placed around expression by some programmers as a<br />

matter of programm<strong>in</strong>g style, but their use is optional.<br />

An appropriate return statement is not enough.When the function declaration is<br />

made, you must also declare the type of value the function returns.This declaration is placed<br />

immediately before the function’s name. Each of the previous examples <strong>in</strong> this book<br />

def<strong>in</strong>ed the function ma<strong>in</strong> to return an <strong>in</strong>teger value, which is why the keyword <strong>in</strong>t is<br />

placed directly before the function name. On the other hand, a function declaration that<br />

starts like this:<br />

float kmh_to_mph (float km_speed)<br />

beg<strong>in</strong>s the def<strong>in</strong>ition of a function kmh_to_mph, which takes one float argument called<br />

km_speed and which returns a float<strong>in</strong>g-po<strong>in</strong>t value. Similarly,<br />

<strong>in</strong>t gcd (<strong>in</strong>t u, <strong>in</strong>t v)

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

Saved successfully!

Ooh no, something went wrong!