Subprograms
- Subprograms come in 2 "flavors", functions and procedures.
- Subprograms do one task and only one task. The task may be simple or
complex but is a single task. Examples: calculate the area of a rectangle,
determine what move the computer should make in a game, print the
rules of a game, get an integer between 2 and 4, inclusively, from the
user, show a menu, etc. If the description of the task includes the
word "and" it is likely doing more than one task and should be broken
into 2 or more subprograms, each of which will do one of the specified
tasks. Examples of subprograms that should be broken down: Calculate the
area and perimeter of a rectangle, show the user a menu and get his/her
choice, find the sum and average of all the values in an array, find the
low value and high value in an array, etc.
Functions
- Functions, ideally, adhere to the mathematic definition of a function. Any
subprogram that doesn't fit that definition is a procedure.
- Functions have NO side effects.
This means that:
- The values of the actual parameters are NOT changed by making changes
to the formal parameters. Essentially, if all formal parameters are
passed by value (in only in Ada/Pascal terms), then there
is no way to change the value of the actual parameter.
- The function makes no change to a value of any global variable or any
other non-local variable that is in it's scope. In C, you only have
to be concerned with globals since you have to work at getting non-
local variables.
- Functions receive all the information they need to do their job through
their parameters. They do NOT get or give information to the user
through any method including input/output.
- They may call on other functions to help complete their ONE task.
They may NOT call on procedures to help complete their tasks.
- Function return precisely one value to the calling program part. The
value that is being returned should be used, either saved in a variable
or at least printed by the program. In general, a function can be
substituted in any location in the program where an expression is
acceptable. In practice, functions are frequently on the right side
of an assignment operator. Ex. area = calc_area_rect(length, width);
- Functions add to the set of operators in a language.
Procedures
- Anything that won't fit the definition of a function will be considered
a procedure. In C, this means that it will be a void function. It will
return nothing.
- All information that needs to be given back to the calling program part
comes back through the parameter list. A procedure can use information
that has been passed by value or passed through some reference or
addressing method.
- In C, in order to get information out of a procedure, an address must be
passed into the procedure. In other languages, the parameters that return
values to the calling program part and often designated in some way such as
in out or out in Ada or var in Pascal.
- Since a procedure does not return a value through a return statement,
it does not substitute for an expression. It acts like an independent
statement and is usually on a line by itself. Ex. get_int(&value);
- Procedures add to the set of statements in a language.