Sams Teach Yourself C in 21 Days

(singke) #1
value of 65.11. When the program runs, it prints the correct number for each. The values
inxandyare passed into the argument kofhalf_of(). This is like copying the values
fromxtok, and then from ytok.half_of()then returns this value after dividing it by 2
(line 27).

108 Day 5

FIGURE5.4
Each time a function is
called, the arguments
are passed to the func-
tion’s parameters.

First function call z=half_of(x);

float half_of(float k)

float half_of(float k)

Second function call z=half_of(y);

3.5

65.11

The Function Body ........................................................................................

Thefunction bodyis enclosed in braces, and it immediately follows the function header.
It’s in the function body that the real work is done. When a function is called, execution
begins at the start of the body and terminates (returns to the calling program) when a
returnstatement is encountered or when execution reaches the closing brace.

Local Variables
You can declare variables within the body of a function. Variables declared in a
function are called local variables. The term localmeans that the variables are
private to that particular function and are distinct from other variables of the same name
declared elsewhere in the program. This will be explained shortly; for now, you should
learn how to declare local variables.
A local variable is declared like any other variable, using the same variable types and
rules for names that you learned on Day 3. Local variables can also be initialized when

DOuse a function name that describes
the purpose of the function.
DOmake sure that the data type of the
arguments you pass to a function match
the data types of the parameters of the
function.

DON’Tpass values to a function that it
doesn’t need.
DON’Ttry to pass fewer (or more) argu-
ments to a function than there are para-
meters. In C programs, the number of
arguments passed much match the num-
ber of parameters.

DO DON’T


NEWTERM

09 448201x-CH05 8/13/02 11:15 AM Page 108

Free download pdf