Let's look at separate compilation in greater detail. We can break a
large program that calls many functions into three pieces.
The three pieces will be compiled separately and combined.
The monolithic program
/* File: big.c
This file uses lots and lots of functions.
*/
#include
/* Prototypes for the functions we use. */
int Sum (int x, int y) ;
int Max (int x, int y) ;
int Min (int x, int y) ;
double Avg (int x, int y) ;
int Dist (int x, int y) ;
main ()
{
int a, b ;
printf("Enter first number: ") ;
scanf ("%d", &a) ;
printf("Enter second number: ") ;
scanf ("%d", &b) ;
printf("\n") ;
printf("The sum of %d and %d is: %d\n",
a, b, Sum (a, b) ) ;
printf("The smaller of %d and %d is: %d\n",
a, b, Min (a, b) ) ;
printf("The larger of %d and %d is: %d\n",
a, b, Max (a, b) ) ;
printf("The average of %d and %d is: %f\n",
a, b, Avg (a, b) ) ;
printf("The distance between %d and %d is: %d\n",
a, b, Dist (a, b) ) ;
printf("That's all folks.\n") ;
}
/* Function: Sum
Returns the sum of x and y.
*/
int Sum (int x, int y)
{
return (x + y) ;
}
/* Function: Max
Returns the larger of x and y.
*/
int Max (int x, int y)
{
if (x > y) {
return (x) ;
} else {
return (y) ;
}
}
/* Function: Min
Returns the smaller of x and y.
*/
int Min (int x, int y)
{
if (x < y) {
return (x) ;
} else {
return (y) ;
}
}
/* Function: Avg
Returns the average of x and y.
Note: Avg returns a *** double *** value.
*/
double Avg (int x, int y)
{
double average ;
average = (x + y) / 2.0 ;
return (average) ;
}
/* Function: Dist
Returns the absolute value of x - y.
*/
int Dist (int x, int y)
{
int distance ;
distance = x - y ;
if (distance < 0) {
return (-distance) ;
} else {
return (distance) ;
}
}
The Sample Run
lassie% cc big.c
lassie% a.out
Enter first number: 2
Enter second number: 5
The sum of 2 and 5 is: 7
The smaller of 2 and 5 is: 2
The larger of 2 and 5 is: 5
The average of 2 and 5 is: 3.500000
The distance between 2 and 5 is: 3
That's all folks.
lassie% a.out
Enter first number: 5
Enter second number: 2
The sum of 5 and 2 is: 7
The smaller of 5 and 2 is: 2
The larger of 5 and 2 is: 5
The average of 5 and 2 is: 3.500000
The distance between 5 and 2 is: 3
That's all folks.
lassie% a.out
Enter first number: 113
Enter second number: 49135
The sum of 113 and 49135 is: 49248
The smaller of 113 and 49135 is: 113
The larger of 113 and 49135 is: 49135
The average of 113 and 49135 is: 24624.000000
The distance between 113 and 49135 is: 49022
That's all folks.