20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

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

pass a negative argument to the squareRoot function. Although this approach might<br />

seem reasonable, it does have its drawbacks. Eventually, you would develop a program<br />

that used the squareRoot function but which forgot to check the argument before call<strong>in</strong>g<br />

the function. If a negative number were then passed to the function, the program<br />

would go <strong>in</strong>to an <strong>in</strong>f<strong>in</strong>ite loop as described and would have to be aborted.<br />

A much wiser and safer solution to the problem is to place the onus of check<strong>in</strong>g the<br />

value of the argument on the squareRoot function itself. In that way, the function is<br />

“protected” from any program that used it. A reasonable approach to take is to check the<br />

value of the argument x <strong>in</strong>side the function and then (optionally) display a message if the<br />

argument is negative.The function can then immediately return without perform<strong>in</strong>g its<br />

calculations. As an <strong>in</strong>dication to the call<strong>in</strong>g rout<strong>in</strong>e that the squareRoot function did not<br />

work as expected, a value not normally returned by the function could be returned. 1<br />

The follow<strong>in</strong>g is a modified squareRoot function, which tests the value of its argument<br />

and which also <strong>in</strong>cludes a prototype declaration for the absoluteValue function as<br />

described <strong>in</strong> the previous section.<br />

/* Function to compute the square root of a number.<br />

If a negative argument is passed, then a message<br />

is displayed and -1.0 is returned. */<br />

float squareRoot (float x)<br />

{<br />

const float epsilon = .00001;<br />

float guess = 1.0;<br />

float absoluteValue (float x);<br />

if ( x < 0 )<br />

{<br />

pr<strong>in</strong>tf ("Negative argument to squareRoot.\n");<br />

return -1.0;<br />

}<br />

while ( absoluteValue (guess * guess - x) >= epsilon )<br />

guess = ( x / guess + guess ) / 2.0;<br />

return guess;<br />

}<br />

If a negative argument is passed to the preced<strong>in</strong>g function, an appropriate message is displayed,<br />

and the value –1.0 is immediately returned to the call<strong>in</strong>g rout<strong>in</strong>e. If the argument<br />

is not negative, calculation of the square root proceeds as previously described.<br />

1.The square root rout<strong>in</strong>e <strong>in</strong> the standard C library is called sqrt and it returns a doma<strong>in</strong> error if a<br />

negative argument is supplied.The actual value that is returned is implementation-def<strong>in</strong>ed. On<br />

some systems, if you try to display such a value, it displays as nan, which means not a number.

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

Saved successfully!

Ooh no, something went wrong!