Help Files: Fortran77: Style





     HELP                      Sep. 15, 1984                 F77/STYLE



        Programming Style: fsplit; include and parameter statements

     This help file discusses several elements  of  programming  style
     that make Fortran programs easier to read, debug, and maintain.

     First it is best to keep each program  in  a  separate  directory
     with each subprogram in a separate file.  This minimizes compila-
     tion time as you will only have to  recompile  those  subroutines
     you have changed.  You can use the 'fsplit' utility to split up a
     source file with many program units into separate files for  each
     program unit, e.g.:

             fsplit bigsource

     Put each common block declaration in a separate file and use  the
     'include'  statement  to  include  the  declaration in the source
     stream.  For example, if you have a common block 'c2' declared in
     several program units as:

                  ...
             integer cnt2, ivec2(20), tot
             common /c2/ cnt2, ivec2, tot
                  ...

     replace the declarations in the program units by:

                  ...
             include 'c2.inc'
                  ...

     and create a two line file 'c2.inc':

             integer cnt2, ivec2(20), tot
             common /c2/ cnt2, ivec2, tot

     This eliminates problems of different common  block  declarations
     in  different  program units, and makes it easier to maintain the
     program.  Use the 'make' utility  to  keep  updating  the  object
     files if you change a common block (see "help f77 make").

     Use parameter statements to define symbolic constants that may be
     used in dimension statements and arithmetic expressions.  You may
     say:

             parameter   (NROWS=20, NCOLS=30, LENBUF=800)
             parameter   (PI=3.1415926535)
             real        a(NROWS,NCOLS), b(NROWS,NCOLS)
             character   buffer(LENBUF)
                             ...
                         do 100 i = 1,NROWS
                             do  90 j = 1,NCOLS
                                a(i,j) = b(i,j)*PI
                             ...

     instead of:

             real        a(20,30),b(20,30)
             character   buffer(800)
                             ...
                         do 100 i = 1,20
                             do  90 j = 1,30
                                a(i,j) = b(i,j)*3.1415926535
                             ...


     Parameters are useful for dimensions, and limits.  This way,  you
     don't have to remember the meaning of each number when you decide
     to change the numerical values of the dimensions.  Parameters can
     also  be  used  for  constants  (e.g.  'PI' above).  Although the
     parameters are written in upper case  letters  in  this  example,
     remember that f77 normally does not distinguish case so that 'PI'
     and 'pi' are the same.  Parameter statements that are repeated in
     several subprogram units should be put into include files.


Comments to decf@euler.berkeley.edu
© 1998 UC Regents