CSC270: C Functions
Functions
Read [King 9.1, 9.2, 9.3]
A function is a block of code that takes parameters, executes some
statements, and (sometimes) returns a value.
float square( float x )
{
return x*x;
}
This function returns the square of its argument and is `called' like
k = square( i );
Function template
function name-----+
| +-------- parameters
return type-+ | ____+___
v v / \
float square( float x )
{ \
return x*x; +---- body
} /
The function body can contain `local variables':
int sum_up_to( int n )
{
int i, sum; <---- local variables
for (i=0; i<=n; i++)
sum += i;
return sum; <---- return statement
}
main()
{
printf( "Sum from 0 to 100 is %d\n", sum_up_to( 100 ) );
}
Local variables are declared at the *top* of the function body. They
are not accessible from program statements outside the function. They
supercede any global variables while the function is being executed.
They are not initialized.
The function must have a return statement whose argument (e.g. `sum'
above) is the value that is returned from the function call. The
return statement can appear anywhere in the function body.
Blocks
In general, a "block" of code is of the form:
{
local variables
statements
}
Any place that uses a block (like a function, or a loop) can declare
local variables at the top of that block.
Parameters versus Arguments
`Parameters' are what appear in the *definition* of a function,
between the parentheses. Above in sum_up_to(), n is a parameter.
`Arguments' are what appear in the *call* of a function, again between
the parentheses. Below, j/3.0 is an argument.
x = sum_up_to( j/3.0 );
Note that parameters are variables names, while arguments are
expressions that have a particular value when the function is called.
Parameterless functions
The parentheses around the parameter and argument lists are necessary,
even if the function doesn't have any parameters:
float pi()
{
return 3.14159;
}
...
x = pi();
Multiple parameters
Multiple parameters are separated by commas. You can mix parameter
types arbitrarily.
float f( float x, int y )
{
return y / x;
}
Void functions
A function might just execute some statement and *not* return a value.
The *type* of such a function is `void'.
void print_lots( int n )
{
int i;
for (i=n; i>0; i--)
printf( "." );
printf( "\n" );
}
main()
{
int i;
for (i=0; i<20; i++)
print_lots( i );
}
A `void' function does not have a `return' statement.
Declaration-before-use and prototypes
A function must be declared *before* it is used. That is, function
declarations must appear closer to the top of the program text than the
calls to those functions. Otherwise, when the function is called, C
will not know how to match up parameters and arguments.
If you don't do this, C will often complain of a `parameter mismatch'.
You can avoid this by using `prototypes'. A prototype is a function
header with NO function body. It just declares what parameters the
function takes and what type of value the function returns, without
providing the code of the function.
float f( float x ); <--- the prototype DECLARATION
main()
{
int i;
for (i=0; i<10; i++)
printf( "f(%d) = %g\n", f( i ) );
}
float f( float x ) <--- the function DEFINITION
{
if (x == 0)
return 5;
else
return 5 * sin(x) / x;
}
The program above has a prototype for function f(). Its presence at
the top of the program allows main() to use f() before the body of f()
appears in the code. Programmers often want main() to appear at the
top because it (usually) gives a good outline of what the program does
and it should immediately be seen by anyone reading the code. Without
the prototype, f() would have to be declared before main() in order for
main() to use it.
Note that prototypes are absolutely necessary for functions that call
each other (e.g. f() calls g(), but also g() calls f()).