mandragora20-ga,
A good example of using user defined functions in such a program would
be the following (actually, two user defined functions are used):
---------- BEGIN PROGRAM ----------
#include <stdio.h>
/* Prints a single line of the table */
void printTableLine (int current) {
printf(" %10d | %10d | %10d \n", current, current*current,
current*current*current);
}
/* Prints the table header and contents */
void printTable (int lower, int upper) {
int i = 0;
printf(" Integer | Square | Cube \n");
printf("------------|------------|------------\n");
for (i = lower; i <= upper; i++) {
printTableLine(i);
}
}
int main (void) {
/* Variable declaration */
int lowerlimit = 0, upperlimit = 0;
/* Retrieve upper and lower bounds; ensure that the lower bound
is less than or equal to the upper bound */
do {
printf("Please enter the lower limit of the table: ");
scanf("%d", &lowerlimit);
printf("Please enter the uppper limit of the table: ");
scanf("%d", &upperlimit);
} while (lowerlimit > upperlimit);
/* Print table */
printTable(lowerlimit, upperlimit);
}
---------- END PROGRAM ----------
The two user defined functions may seem pointless, but they serve to
seperate the program into logical components. Certainly, as you
discovered, you can write the program without these functions, but
compartmentalization in this manner makes it clear what each chunk of
code is doing. The benefits of this become much more noticeable when
the program increases in complexity.
If you have any questions or are unsatisfied with the answer, feel
free to ask for clarification.
Cheers,
tox-ga |