Function Calls and File Structure
This page shows two examples to illustrate functions. It then splits
those functions into separate files.
-
funcAddThree.c - Program that calls a function to perform calculation.
-
three files - hwx.c, hwx.h, test-hwx.c - Program structure required in this class - three 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
- Line 16 declares the function. It tells the name of the function and
provides an ordered list of inputs with names and types. The names are what they will
be called within the function.
- Line 18 shows how to return a value. All functions must explicitly return
a single value. If you forget to return, and the function has a return type, then it
return an unknown value.
- Line 44 calls the function.
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
-
Line 1 contains the first guard. Note the similarity to the file name and the
all capital letters. This says to read what is between this and the #endif only
if HWX_H has not yet been defined. The first time it encounters this line,
HWX_H will not have been defined, so it will be true, and the file will be read.
The purpose is so that when you have large compilations with many files, if the
same .h file is included multiple times, it will only be read by the compiler the
first time. Otherwise, the compiler will think all of the stuff is being defined
multiple times.
-
Line 2 defines HWX_H. This means that next time it encounters the file, the
#ifndef will be false, and the file will be skipped.
-
Lines 3-11 have the function comments
-
Line 12 contains the prototype. It is like the first line of the function
definition except that variable names are not necessary and there is a semicolon
at the end of the line. This is the necessary information for the compiler to
verify that function calls are correct.
-
Line 13 contains the #endif that matches the #ifndef. This signals the end of the
text that will be skipped if #ifndef evaluates to false.
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
-
Line 6 contains a #include "hwx.h" to read in any declarations that occurred in
the header file. In this particular case, this is not necessary. However, it is
not unusual for data structures to be declared in the .h file, in which case it
would be necessary.
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 }
-
Line 6 contains a #include "hwx.h" to read in any declarations that occurred in
the header file. In this particular case, this is not necessary. However, it is
not unusual for data structures to be declared in the .h file, in which case it
would be necessary.