Hardware Reference
In-Depth Information
A complete program to produce a delay would take the form:
/* delay1.c */
main()
{
delay();
}
delay()
{
long x;
for (x = 1; x<200000; ++x);
}
It is important to note that no semicolon follows the closing bracket of a func-
tion definition, whereas when the function is called the program statement is
terminated by a semicolon. The main body of the function is enclosed between
curly braces ({and}). Since C/C
++
is essentially a 'free-form' language (i.e.
the compiler ignores white space within the source text) the programmer is able
to adopt his/her own style of layout within the source text. The C functions
and programs presented in this chapter will, however, follow the convention
adopted by the author summarized below:
•
Matching opening and closing braces, {and}, are vertically aligned with one
another.
•
Statements within the body of a function are indented by three columns with
respect to their opening and closing braces.
•
Expressions (enclosed in brackets) used in conjunction with
for
and
while
statements are placed on the same line as the matching
for
or
while
.
•
Where readability needs to be improved, blank lines are used to separate
function definitions.
•
The first function defined in a program is
main()
.
Returning to the previous example, readers will probably have spotted a
fundamental weakness in the simple delay function arising from the fact that
it is only capable of providing a fixed delay. The function can be made more
versatile by passing a
parameter
into it. The following modified delay function
achieves this aim:
delay(limit)
long limit;
{
long x;
for(x = l; x < limit; ++x);
}
The
argument
(contained in parentheses after the function name) is defined
as a
long
type before the function body. The function is then called using a
statement of the form:
delay(200000);
Thereafter, the value 20 000 is passed to the function and is used as the value
for
limit.
A simple delay program would then take the form:
/* delay2.c */
main ()