Owner`s manual

100
Arguments
Arguments are used to pass values to the function when it is called. If no values are
passed, the arguments can be omitted. If the arguments are omitted, the following
argument type declaration is also omitted.
Argument type declaration
Declares the types of the arguments specified above.
Note that ANSI C allows declaring arguments directly inside the parenthesis. The C
interpreter does not allow such a declaration.
Statements
The portion of the function between the braces is executed when the function is
called. Use the same format as that which we have seen for our main() programs.
EXAMPLE 1:
For illustrative purposes, let’s modify the program that squares all integers from 1 to
100 (see page 97) by performing the squaring calculation with a function.
/* 100 squares 2 */
/* #include <stdio.h> */
int isquare(x) /* square of int */
int x;
{
return (x*x);
}
main(){
int i;
for (i=1; i<=100; i++)
printf(“%8d %8d¥n”,i,isquare(i));
getchar(); /* waits ret key */
}
Returned value
The function isquare() that we have defined receives the argument, squares it, and
then returns the squared value. The line “return(i*i)” instructs the computer to square
the value of “x” and return it to the main() function.
When the main() function executes isquare(i), the value stored in the variable “I” is
passed to function isquare() as argument “x”. Note that the value only is passed, and
that variables x and i are different variables. There is no way for the function
isquare() to modify the content of variable i.
The “return” statement within isquare() will give the value of the square to the
expression isquare(i) in the main() function, and execution precedes to the printf
statement for output.
EXAMPLE 2:
We will rewrite the same program using now a function that returns floating-point
values.