CS 455 Lecture Notes
Week Four, Thursday: Functions
Use existing library functions when possible.
Math Library Functions
These include logarithm, exponential, and power functions; trigonometric functions; and
floating point and integer operation functions (floor, ceiling, and absolute value).
Declarations (Function Prototypes) and Definitions
Always use function prototypes (although they are not required by the compiler
if they are defined before they are called). In large programs, function prototypes
should be declared in a header (*.h) file.
Pseudo-Random Number Generation
Create a random integer over a defined range:
rand() % 6
for example.
Scope Issues
Never use global variables. Global constants are OK.
Keyword static example:
void f1()
{
static int count = 1; // Initialized only the first time f1() is called.
int i = 1; // Initialized every time f1() is called.
count++;
} // i goes "out of scope" but count does not.
Recursion
Example: Fibonacci numbers.
Function Overloading
Different functions can have the same name if their parameter lists are different.
Function Templates
Another way to get the effect of function overloading is with function templates.
This page established September 19, 1999; last updated September 19, 1999.