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.

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

After the value of the greatest common divisor has been calculated by the gcd function,<br />

the statement<br />

return u;<br />

is executed.This has the effect of return<strong>in</strong>g the value of u,which is the value of the<br />

greatest common divisor, back to the call<strong>in</strong>g rout<strong>in</strong>e.<br />

You might be wonder<strong>in</strong>g what you can do with the value that is returned to the call<strong>in</strong>g<br />

rout<strong>in</strong>e. As you can see from the ma<strong>in</strong> rout<strong>in</strong>e, <strong>in</strong> the first two cases, the value that is<br />

returned is stored <strong>in</strong> the variable result. More precisely, the statement<br />

result = gcd (150, 35);<br />

says to call the function gcd with the arguments 150 and 35 and to store the value that<br />

is returned by this function <strong>in</strong> the variable result.<br />

The result that is returned by a function does not have to be assigned to a variable, as<br />

you can see by the last statement <strong>in</strong> the ma<strong>in</strong> rout<strong>in</strong>e. In this case, the result returned by<br />

the call<br />

gcd (83, 240)<br />

is passed directly to the pr<strong>in</strong>tf function, where its value is displayed.<br />

A C function can only return a s<strong>in</strong>gle value <strong>in</strong> the manner just described. Unlike<br />

some other languages, C makes no dist<strong>in</strong>ction between subrout<strong>in</strong>es (procedures) and<br />

functions. In C, there is only the function, which can optionally return a value. If the<br />

declaration of the type returned by a function is omitted, the C compiler assumes that<br />

the function returns an <strong>in</strong>t—if it returns a value at all. Some C programmers take<br />

advantage of the fact that functions are assumed to return an <strong>in</strong>t by default and omit<br />

the return type declaration.This is poor programm<strong>in</strong>g practice and should be avoided.<br />

When a function returns a value, make certa<strong>in</strong> you declare the type of value returned <strong>in</strong><br />

the function’s header, if only for the sake of improv<strong>in</strong>g the program’s readability. In this<br />

manner, you can always identify from the function header not only the function’s name<br />

and the number and type of its arguments, but also if it returns a value and the returned<br />

value’s type.<br />

As noted earlier, a function declaration that is preceded by the keyword void explicitly<br />

<strong>in</strong>forms the compiler that the function does not return a value. A subsequent attempt<br />

at us<strong>in</strong>g the function <strong>in</strong> an expression, as if a value were returned, results <strong>in</strong> a compiler<br />

error message. For example, because the calculateTriangularNumber function of<br />

Program 8.4 did not return a value, you placed the keyword void before its name when<br />

def<strong>in</strong><strong>in</strong>g the function. Subsequently attempt<strong>in</strong>g to use this function as if it returned a<br />

value, as <strong>in</strong><br />

number = calculateTriangularNumber (20);<br />

results <strong>in</strong> a compiler error.<br />

In a sense, the void data type is actually def<strong>in</strong><strong>in</strong>g the absence of a data type.Therefore,<br />

a function declared to be of type void has no value and cannot be used as if it does have<br />

a value <strong>in</strong> an expression.

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

Saved successfully!

Ooh no, something went wrong!