HP Fortran Programmer's Reference (September 2007)

Data types and data objects
Derived types
Chapter 5 129
PRINT *,’Heavy commute on rte.’, route(choice)%rte_num
ELSE
PRINT *,’Easy commute on rte.’, route(choice)%rte_num
END IF
END PROGRAM traffic
LOGICAL FUNCTION busy(when)
! This function accepts a derived-type argument whose definition
! is defined in the module hours_def, made accessible here by
! use association. It returns .TRUE. or .FALSE., depending on
! on the value of the user-selected component of the argument.
! Make the definition of hours accessible to this function.
USE hours_def
TYPE(hours) :: when
INTEGER :: choice
PRINT *, 'When do you want to commute:'
PRINT *, '1. Morning'
PRINT *, '2. Evening'
READ *, choice
! Find out if the route is busy at that time of day.
IF (choice .EQ. 1) THEN
busy = when%am
ELSE
busy = when%pm
END IF
END FUNCTION busy
MODULE hours_def
! Define a derived type, which will be passed as an argument.
TYPE hours
LOGICAL :: am
LOGICAL :: pm
END TYPE hours
END MODULE hours_def
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 traffic.f90
$ a.out
What road do you want to travel?
1. Rte. 128
2. Rte. 93
3. Rte 97