CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C C F O R T _ S U B . F C C C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C Here is a module that contains an example of a FORTRAN subroutine C and its unit test driver. The pre-processor is used to handle C #defines and conditional compilation. C C Uncomment the following #define if you want DEBUG info. C /* #define DEBUG */ C C Include some global constants. The Pre-processor can be used to C physically include the text from another file at the location of C the '#include'. Here we're loading in a file of constants that C might be used in this routine. Using this 'include' feature allows C you to collect all of your #defines together in one file rather than C having then sprinkeled along in the tops of lots of files. C #include "const.h" #define OUTFILE 'fout.dat' /* Fortran Data Output File */ #define NPTS 3 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C C UNIT Test driver to test the subroutine fort_sub C C C C Standalone main program to test routine: 'fort_sub' C C C C Parameter: C C C C C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC #ifdef UNIT_TEST PROGRAM SUB_UNIT REAL PARAM1 INTEGER UNIT UNIT = 6 PARAM1 = 666.0 write(6,*)'------------- FORTRAN SUBROUTINE UNIT TEST ROUTINE -----------' write(6,*)'Unit Testing the Fortran subroutine with parameter: ',PARAM1 write(6,*) '' C C Call the subroutine being tested with a variety of arguments C CALL FORT_SUB(PARAM1, UNIT) write(6,*)'Subroutine returned' END #endif CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C C SUBROUTINE FORTRAN_SUBROUTINE C C C C Test subroutine to illustrate FORTRAN code C C C C Parameter: C C C C arg - The first argument passed into the routine.... C C unit - The unit where the output should go C C C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC SUBROUTINE FORT_SUB(arg, c_unit) REAL arg INTEGER c_unit REAL*8 X(NPTS) INTEGER I C/* C * Write some stuff to the ouput unit specified in the calling sequence. C * If the output unit hasn't been associated with a filename, this C * f77 compiler uses the convention of creating a file named, 'fort.#' C * where the number is specified in the calling sequence. C */ write(c_unit,*)'-------------' write(c_unit,*)'Entering the Fortran routine with parameter: ',arg write(c_unit,*) '' arg = COS(arg) write(c_unit,*)'Replacing the argument with its cosine: ',arg write(c_unit,*)'-------------' C C Here's a swatch of debugging code hidden behind the pre-processor C directive 'DEBUG'. If this directive is set with '#define DEBUG' or set with the C compiler flag '-DDEBUG' , then this code will become active. C #ifdef DEBUG C/* C * Here some debugging code... C */ 500 CONTINUE write(c_unit,*) 'Put your debugging code in here' DO 510 I = 1, NPTS write(c_unit,*) x(i) 510 CONTINUE #endif RETURN END