DMP - Chapter 9
Functions
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
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)
Go to Ich's Home Page
Go to the Peabody Home Page
For comments or questions email to:
ich@peabody.jhu.edu