|
|||
|
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:
Where:
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:
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;
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 |