HP Fortran Programmer's Guide (B3908-90031; September 2011)

Compiling and linking
Special-purpose compilations
Chapter 2 87
item(n_items) = new_item
sum = sum + new_item
END SUBROUTINE update_db
! get_avg: returns arithmetic mean
INTEGER FUNCTION get_avg ()
get_avg = sum / n_items
END FUNCTION get_avg
END MODULE stats_code
This program unit also begins with a USE statement, which identifies the module it uses as stats_db. This
module is defined in data.f90, as follows:
Example 2-4 data.f90
! stats_db: shared data declared here
MODULE stats_db
INTEGER, PARAMETER :: size = 100 ! max number of items in array
! n_items, sum, and item hold the data for statistical analysis
INTEGER :: n_items, sum
INTEGER, DIMENSION(size) :: item
! the initializations are just to start the program going
DATA n_items, sum, item/3, 233, 97, 22, 114, 97*0/
END MODULE stats_db
The use of modules in this program creates dependencies between the files because a file that uses a module
that is defined in another file is dependent on that other file. These dependencies affect the order in which
the program files must be compiled. The dependencies in the example program are:
main.f90 is dependent upon code.f90.
code.f90 is dependent upon data.f90.
These dependencies require that data.f90 be compiled before code.f90, and that code.f90 be
compiled before main.f90. This order ensures that the compiler will have created each of the .mod files
before it needs to read them.
The order of the source files listed in the following command line ensures that they will compile and link
successfully:
$ f90 -o do_stats data.f90 code.f90 main.f90
During compilation, f90 will create two .mod files, STATS_CODE.mod and STATS_DB.mod. These will
be written to the current working directory, along with the object files and the executable program,
do_stats. Following is a sample run of the executable program:
$ do_stats
Enter an integer (hint: 77 is current average): 77
Average or better.