18
Chapter 1
Calling Functions
To call (or
invoke) a function, you use the
name of the desired function,
parentheses, and a comma-separated list of the required parameters. The
compiler reads files from top to bottom, so the function’s declaration must
appear before its point of first use.
Consider the program in Listing 1-7, which uses the
step_function
.
int step_function(int x) {
--
snip
--
}
int main() {
int value1 = step_function(100); // value1 is 1
int value2 = step_function(0); // value2 is 0
int value3 = step_function(-10); // value3 is -1
}
Listing 1-7: A program using the
step_function
. (This program produces no output.)
Listing 1-7 calls
step_function
three times with different arguments and
assigns the
results to the variables
value1
,
value2
, and
value3
.
Wouldn’t it be nice if you could print these values? Fortunately, you can
use the
printf
function to build output from different variables. The trick is
to use
printf
format specifiers.
Dostları ilə paylaş: