CH 3  SUBPROGRAMS


Subprograms: Functions and Procedures

PROCEDURES  (a.k.a. "void functions" in C)

Example
void  DisplayTriangle(void)

Purpose
To simplify the organization of a solution via decompostion

Application
Used for more complicated tasks or when multiple values result (Ch 6).
No return value.
May do input/output with user.
Invoked like a statement.

Example

Fig 3.14 - Draw Stick Figure

FUNCTIONS (a.k.a. "value returning functions" in C)

Example
double  CalculateDistance(double speed, double time)

Purpose
1. To eliminate redundant code. (Hypotenuse Example)
2. To facilitate code reuse

Application
Used for simple, single purpose tasks that compute a single result.
Invoked on the right hand side of an assignment statement, in a print statement, or as part of an expression.
In general, functions should not perform input/output with user.

Example

Fig 3.24 - Scale Function


Exercise
Prediction Exercise Ch3_1.c