Function Calls and File Structure

This page shows two examples to illustrate functions. It then splits those functions into separate files.

Calling a Function

1	/*  functiontemp.c 
2	 * purpose: Show how to implement, use functions.
3	 */  
4	#include <stdio.h>
5	#include <stdlib.h>
6
7	/* add three numbers
8	 * purpose: adds three numbers
9	 *  input parameters:
10	 *   float - first number
11	 *   double - second number
12	 *   double - third number
13	 *  return value:
14	 *   float - the sum of the three numbers
15	 */
16	float add_three(float first, double second, double third)
17	{
18	        return first + second + third;
29	}
20
21	int main() 
22	{     
23		// Declare variables to be used
24		int number;                             // The integer
25		float fpNumber = 0.0f;                  // The floating point number
26		double dpNumber = 0.0, total = 0.0;     // The double and the total
27
28		// Explain to the user what the program does
29		printf( "This program adds together two floating point numbers\n" );
30		printf( "and an integer.\n" );
31		printf( "Written January 2009 by John Bell for CS 107.\n\n" );
32
33		// Get user input ( and check it if possible )
34		printf( "Please enter the first floating point number > " );
35		scanf( "%f", &fpNumber );
36
37		printf( "\nPlease enter the second floating point number > " );
38		scanf( "%lf", &dpNumber );
39
40		printf( "\nPlease enter the integer > " );
41		scanf( "%d", &number );
42
43		// Perform necessary calculations
44		total = add_three(fpNumber, dpNumber, number);
45
46		// Report results ( in a complete and well-formed format. )
47		printf( "\nThe total of %f plus %f plus %d is %f\n", 
48			fpNumber, dpNumber, number, total );
49	        // return success
50	        return (0);
51        
52	 }

Explanation of Specific Lines

Splitting Program into Three Files

In C, any program of appreciable size is split into multiple files. Once split, the smallest number of files is 3. Main is in one file. Functions are in another file, and the prototypes of the functions are in a third file. For the purposes of this class, we will have naming conventions for these files. This example splits the code from the previous example into the three files.

hwx.h - contains comments and prototypes of all functions.

1	#ifndef HWX_H
2	#define HWX_H
3	/* add_three
4	 * purpose: adds three numbers
5	 *  input parameters:
6	 *   float - first number
7	 *   double - second number
8	 *   double - third number
9	 *  return value:
10	 *   float - the sum of the three numbers
11	 */
12	float add_three(float, double, double);
13	#endif

hwx.c - contains function implementations (definitions)

1	/*  hwx.c
2	 * purpose: Provide functions that will be called from another file.
3	 */  
4	#include <stdio.h>
5	#include <stdlib.h>
6	#include "hwx.h"
7	/* add_three
8	 * purpose: adds three numbers
9	 *  input parameters:
10	 *   float - first number
11	 *   double - second number
12	 *   double - third number
13	 *  return value:
14	 *   float - the sum of the three numbers
15	 */
16	float add_three(float first, double second, double third)
17	{
18	        return first + second + third;
29	}
20

test-hwx.c - this contains main and any testing functions

1	/*  test-hwx.c
2	 * purpose: Show how to call functions defined in another file.
3	 */  
4	#include <stdio.h>
5	#include <stdlib.h>
6	#include "hwx.h"
7
8	int main() 
9	{     
10		// Declare variables to be used
11		int number;                             // The integer
12		float fpNumber = 0.0f;                  // The floating point number
13		double dpNumber = 0.0, total = 0.0;     // The double and the total
14
15		// Explain to the user what the program does
16		printf( "This program adds together two floating point numbers\n" );
17		printf( "and an integer.\n" );
18		printf( "Written January 2009 by John Bell for CS 107.\n\n" );
19
20		// Get user input ( and check it if possible )
21		printf( "Please enter the first floating point number > " );
22		scanf( "%f", &fpNumber );
23
24		printf( "\nPlease enter the second floating point number > " );
25		scanf( "%lf", ∓dpNumber );
26
27		printf( "\nPlease enter the integer > " );
28		scanf( "%d", ∓number );
29
30		// Perform necessary calculations
31		total = add_three(fpNumber, dpNumber, number);
32
33		// Report results ( in a complete and well-formed format. )
34		printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
35				dpNumber, number, total );
36	        // return success
37	        return (0);
38        
39	 }