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.

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

Based upon this discussion, you can understand that when the value of guess 2 - x is<br />

passed to the absoluteValue function and assigned to the formal parameter x, this<br />

assignment has absolutely no effect on the value of x <strong>in</strong>side the squareRoot function.<br />

Declar<strong>in</strong>g Return Types and Argument Types<br />

As mentioned previously, the C compiler assumes that a function returns a value of type<br />

<strong>in</strong>t as the default case. More specifically, when a call is made to a function, the compiler<br />

assumes that the function returns a value of type <strong>in</strong>t unless either of the follow<strong>in</strong>g has<br />

occurred:<br />

1. The function has been def<strong>in</strong>ed <strong>in</strong> the program before the function call is encountered.<br />

2. The value returned by the function has been declared before the function call is<br />

encountered.<br />

In Program 8.8, the absoluteValue function is def<strong>in</strong>ed before the compiler encounters<br />

a call to this function from with<strong>in</strong> the squareRoot function.The compiler knows, therefore,<br />

that when this call is encountered, the absoluteValue function will return a value<br />

of type float.Had the absoluteValue function been def<strong>in</strong>ed after the squareRoot<br />

function, then upon encounter<strong>in</strong>g the call to the absoluteValue function, the compiler<br />

would have assumed that this function returned an <strong>in</strong>teger value. Most C compilers<br />

catch this error and generate an appropriate diagnostic message.<br />

To be able to def<strong>in</strong>e the absoluteValue function after the squareRoot function (or<br />

even <strong>in</strong> another file—see Chapter 15), you must declare the type of result returned by the<br />

absoluteValue function before the function is called.The declaration can be made <strong>in</strong>side<br />

the squareRoot function itself, or outside of any function. In the latter case, the declaration<br />

is usually made at the beg<strong>in</strong>n<strong>in</strong>g of the program.<br />

Not only is the function declaration used to declare the function’s return type, but it<br />

is also used to tell the compiler how many arguments the function takes and what their<br />

types are.<br />

To declare absoluteValue as a function that returns a value of type float and that<br />

takes a s<strong>in</strong>gle argument, also of type float, the follow<strong>in</strong>g declaration is used:<br />

float absoluteValue (float);<br />

As you can see, you just have to specify the argument type <strong>in</strong>side the parentheses, and<br />

not its name.You can optionally specify a “dummy” name after the type if you want:<br />

float absoluteValue (float x);<br />

This name doesn’t have to be the same as the one used <strong>in</strong> the function def<strong>in</strong>ition—the<br />

compiler ignores it anyway.<br />

A foolproof way to write a function declaration is to simply use your text editor to<br />

make a copy of the first l<strong>in</strong>e from the actual def<strong>in</strong>ition of the function. Remember to<br />

place a semicolon at the end.

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

Saved successfully!

Ooh no, something went wrong!