CSC270 Sept 23, Lecture II: And even more C

```
Functions

[King 9]

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 i;                <---- 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.

The function must have a return statement whose argument (e.g. `i'
above) is the value that is returned from the function call.

Definition-before-use and prototypes

A function must be defined *before* it is used.  That is, function
definitions 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 );

main()

{
int i;

for (i=0; i<10; i++)
printf( "f(%d) = %g\n", f( i ) );
}

float f( float x )

{
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 defined 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()).

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.

```