Sams Teach Yourself C in 21 Days

(singke) #1
As you can see in this listing, there is only one rectangle()declaration and
function being used. This function differs from previous examples by using
default values. The function prototype in line 5 contains values being assigned to each of
the parameters. These values are considered default values.
In line 10, you see that the rectangle()function is called in the normal way with each
of its three parameters being passed. In line 13, however, you see that the draw_char
value has been left off. In the output, you can see that the rectangle is still drawn using
an‘X’. The ‘X’character is the default value set in line 5. In lines 16 and 19, the rectan-
gle function is called again, each time with one less parameter. In each case, the default
parameters are pulled from the prototype in line 5.
When using default values, it is always assumed that the values presented are valid from
the left to the right. For example, using the rectangle()function above, you cannot pass
a display character without passing a width and length. The following would generate
bad results:
rectangle( ‘*’ );
This call to rectangle()uses the ASCII value of ‘*’for the width rather than for the
display character. Using the rectangle()function declared in line 5 of Listing B2.5, the
first parameter of the rectangle function is always the width, the second is always the
length, and the third is the display character. You might believe that the following
rectangle ( , , ‘*’);
would fix this problem; however, this will generate an error. You must pass actual para-
meters.

658 Bonus Day 2

ANALYSIS

You should place values that will probably be defaulted as far to the right in
Tip the argument list as possible.

Inline Functions ............................................................................................


Inline functions are also a feature of C++ that is not available in C. An inline function is
like any other function. The difference lies in how the compiler treats the function. By
declaring a function as inline, you are making a request to the compiler. You are request-
ing that the compiler replace all calls to the inline function with a copy of the code
within the inline function.
Inline functions are used simply for speed. Each time you call a function, a little bit of
time is used to go to and return from the function. By declaring a function as inline, you

37 448201x-Bonus2 8/13/02 11:18 AM Page 658

Free download pdf