HP Fortran Programmer Guide (766160-001, March 2014)
Examples
Example 5
Consider, for example, a program that consists of three files: main.f90, code.f90, anddata.f90
. The main program unit is in main.f90, as follows.
Example 6 Example 2-2 main.f90
PROGRAM keep_stats
! stats_code contains module procedures for operating
! on statistical database
USE stats_code
INTEGER :: n
! print prompt, using nonadvancing I/O
WRITE (*, FMT='(A)', ADVANCE='NO') 'Enter an integer '// &
'(hint: 77 is current average): '
READ *, n
IF (n == 0) THEN
PRINT *, 'But not that one.'
ELSE
CALL update_db(n)
IF (n >= get_avg()) THEN ! get_avg is in stats_code
PRINT *, 'Average or better.'
ELSE
PRINT *, 'Below average.'
END IF
END IF
END PROGRAM keep_stats
The first specification statement (USE) in the main program indicates that it uses the module
stats_code. This module is defined in code.f90, as follows:
Example 7 Example 2-3 code.f90
! stats_code: a (partial!) package of module procedures for
! performing statistical operations
MODULE stats_code
! shared data to be used by procedures declared below
USE stats_db
CONTAINS ! module procedures
! update_db: updates shared variables in module stats_db
SUBROUTINE update_db (new_item)
INTEGER :: new_item
n_items = n_items +1
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 USEstatement, which identifies the module it uses as stats_db.
This module is defined in data.f90, as follows:
56 Compiling and linking