HP Fortran Programmer's Reference (September 2007)

Data types and data objects
Derived types
Chapter 5128
A derived-type example
The example below, traffic.f90, illustrates how to define a derived type, declare a variable of
the type, specify a value for the variable using the structure constructor, pass the variable as
an argument to another procedure, and reference a structure component. The derived type is
defined in a module so that it can be made accessible by use association.
For more information about modules and the USE statement, see “Modules” on page 190. The
MODULE and USE statements are also described in Chapter 10.
Example 5-3 traffic.f90
PROGRAM traffic
! Illustrates derived types: defines a derived type, declares an
! to array variable of derived type, uses a structure constructor
! assign to its components, and passes a component which is
! itself another derived type to a subprogram.
! Make the definition of the derived type called hours accessible
! to this program unit
USE hours_def
LOGICAL :: busy
INTEGER :: choice
! Define another derived type that uses hours as a component
TYPE hiway
INTEGER :: rte_num
TYPE(hours) :: busy_hours
END TYPE hiway
! Declare an array of derived-type structures.
TYPE(hiway), DIMENSION(3) :: route
! Use the structure constructor to specify values for each
! element of route
route(1) = hiway(128, hours(.TRUE., .FALSE.))
route(2) = hiway(93, hours(.FALSE., .TRUE.))
route(3) = hiway(97, hours(.FALSE., .FALSE.))
PRINT *, 'What road do you want to travel?'
PRINT *, '1. Rte. 128'
PRINT *, '2. Rte. 93'
PRINT *, '3. Rte 97'
READ *, choice
! Pass the busy_hours component of the selected route to
! the function busy.
IF (busy(route(choice)%busy_hours)) THEN