San Diego State University logo
ME 203

Module 9 - Files II


 

 

In this module, we'll refer to files found in the tar file module9.tar.

As programs become more complex, performing a wider variety of functions and incorporating a greater number of tasks, placing all of the function definitions into one files becomes increasingly cumbersome.

The C programming language provides facilities that allow programmers to incorporate several files in one compilation call; this feature facilitates the definition of numerous functions into a file, and the access of functions by programs in separate files.

In order to use a function defined in another file, the program accessing the function still must know how to interface with the function; what arguments it takes, and what, if any, value it returns as well as the data type of that returned value. We do this by using function prototypes. Function prototypes take the following form:

return-type fn-name (dtype1 , dtype2 , ..., dtypen);

Where:

  • return-type is the data type of the value returned from this function (If no value is to be returned, use void for the return-type).
  • fn-name is the name of the function
  • the list of dtypen are the data type for the values that are expected by fn-name, and passed to it by any calling function or program.
  • remember to terminate the protoype with a semi-colon ;

C compilers are by design 'smart' about translating C code into machine-readable assembly language and optimizing that translated code, but not so smart when it comes to figuring out the intent of the programmer, hence the need for rigor when writing a C program, and the need for these function prototypes.

In fact, if the function you were calling to in the program is defined in the same file, but after the calling program (i.e., lower in the file than the calling program), you would still need to include a function prototype at the top so that the C compiler could translate the code properly.

Let's take a look at a basic use of function prototypes for functions defined in separate files. module9.tar contains basically equivalent functions for those from module5, save for changes to incorporate funcction definition is separate files. We'll start with the vector/vector summation program found in vvs.c.

If you compare this program with vvs.c found in module 5, you'll find few differences; there are some minor programmatic changes, but the only significant difference is that the actual summation mechanism has been removed from the program, and defined as a function in it's own file, vvs_summation.c.

As a result, in line 11 of vvs.c, a function prototype for that remotely defined function is set:

void vector_sum(float* , float* , float* , int ) ;

And we remove lines 69 through 71 (Actually, they are commented out) in favor of the function call to vector_sum in line 64. If we open the file vector_sum.c, where the function is defined, we see those commented out lines 69 to 71, wrapped in the function definition for vector_sum.

Otherwise, the program runs pretty much identically to the old vvs.c;

  • Lines 13 thru 23 define the variables used (file input/output pointers fvi1, fvi2, and fvo1, pointers to floating point variables used in the vector sum process v1, v2 and vs, and integers i, n1, n2, and n to track positions in the vectors) in the program
  • In lines 24 through 36, the file pointers are assigned files, the files are opened and tested to make sure they opened correctly.
  • Conditionals test to make sure vector sizes match and are not too large in lines 37 through 50.
  • The pointers to floats v1, v2, and vs are allocated appropriate memory vector values are scanned into the vectors in lines 52 to 61.
  • Line 64 marks the major change in this version of vvs.c; we make the call to the function vector_sum, passing it the 2 vectors used to calculate the sum, v1 and v2, the result vector vs, and the vector size n. (Lines 66 to 72 contain the commented out vector sum calculation, now contained in the function vector_sum defined in the file vector_sum.c
  • Lines 74 through 77 define the for loop that prints out the elements in the result vector vs.
  • We close the files in lines 79 to 81 and the program terminates.

The above was a very simple implementation of 1 function defined in a file separate from the main program. The Lower/Upper method for calculating the ?? points of a function defined in the subdirectory LU of module9 implements

Website by Joshua Bleier