A reusable function, which is a good general module should be kept with other reusable functions about that same topic. You can then name the file appropriately and save it for use in future projects that deal with that topic.

So you will end up with more than one source file:

- One will contain main( ), and the functions that deal specifically with this current project.
- Another source file will contain function definitions that deal with a certain topic. Hopefully these functions will be designed to be as general as possible.
- A third file is the header file that contains the function prototypes for the previously mentioned general function definitions.

- The name of the file that contains main( ) and the functions specific to the current project is typically called proj?.c, where ? is some number.
- The name of the file with the general functions' definitions should reflect the shared topic and must have a .c extension. (i.e. stats.c).
- The associated header file carries the same name, but has a .h extension (i.e. stats.h)

The source code pieces will be compiled separately to make their own object code files. The object files can then be combined by the linker along with other necessary object files to make the executable file.

/******************************************** * File: big.c * Author: S. Bogar * Date: 8/3/99 * Section: 101 * EMail: bogar@cs.umbc.edu * * This file uses lots and lots of functions. *********************************************/ #include <stdio.h> /* 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) ; int main () { int a, b ; /* get two ints from the user */ printf("Enter first number: ") ; scanf ("%d", &a) ; printf("Enter second number: ") ; scanf ("%d", &b) ; printf("\n") ; /* use functions for perform simple arithmetic. ** print the results of each function call */ 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) ) ; /* we're done!! */ printf("That's all folks.\n") ; return 0; } /******************************************** * Function: Sum * Usage: z = Sum (x, y) * * This function adds the two values passed in * and returns their sum. * * Input: two integers to be added * Output: Returns the sum of x and y. *********************************************/ int Sum (int x, int y) { int sum ; sum = x + y ; return sum ; } /********************************************* * Function: Max * Usage: z = Max (x, y) * * This function determines which of the values * passed in is larger. * * Inputs: two integers to be compared * Output: Returns the larger of x and y. *********************************************/ int Max (int x, int y) { int max ; if (x > y) { max = x ; } else { max = y ; } return max ; } /********************************************* * Function: Min * Usage: z = Min (x, y) * * This function determines which of the values * passed in is smaller. * * Inputs: two integers to be compared * Output: Returns the smaller of x and y. *********************************************/ int Min (int x, int y) { int min ; if (x < y) { min = x ; } else { min = y ; } return min ; } /********************************************* * Function: Avg * Usage: z = Avg (x, y) * * This function calculates the average of the * two values passed in. * * Inputs: two integers to be compared * Output: Returns average of x and y as a *** double *** * *********************************************/ double Avg (int x, int y) { double average ; average = (x + y) / 2.0 ; return average ; } /********************************************* * Function: Dist * Usage: z = Dist (x, y) * * This function calculates the distance between * x and y on a number line. * * Input: two integers for which distance is calculated * Output: Returns the absolute value of x - y. *********************************************/ int Dist (int x, int y) { int distance ; distance = x - y ; if (distance < 0) { distance = -distance ; } return distance ; }

linux2[75] % gcc -Wall -ansi big.c linux2[76] % 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. linux2[77] % !a 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. linux2[78] % !a 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. linux2[79] %