Use function:
- to do one task
- to avoid writing duplicate code
- to make code modular, hence easier to read and debug
Local variable, global variable, static variable
#define LIMIT 40
void separator(void)
{
int i;
for (i = 0; i < LIMIT; i++)
putchar('*');
}
Function arguments
void separator_1(int total)
{
int i;
for (i = 0; i < total; i++)
putchar('*');
}
void separator_2(int total, char ch)
{
int i;
for (i = 0; i < total; i++)
putchar(ch);
}
Functions returning a value
float fmax(float x, float y)
{
if (x >= y) /* OR return((x >= y) ? x : y); */
return(x);
else
return(y);
}
Function type,
declaration, prototype, and definition
Recursion (postponed)
Multiple functions / files
Address operator: & (Lisitng 9.13)
Pointer type stores addresses: e.g. int *ptr
Indirection operator: * (Lisitng 9.13a)
when followed by an address, * gives the value stored at that address.
Passing arguments to functions
by address (swap.c)
Assignment #9: Due Nov. 30, 1995.
p.317-8: #1, 3, 5, 7, 9 (refer to Listing 9.9 not 9.13)
Go to Ich's Home Page
Go to the Peabody Home Page
For comments or questions email to:
ich@peabody.jhu.edu