HP Fortran Programmer's Reference (September 2007)
Data types and data objects
Derived types
Chapter 5 123
Derived types
Derived types are user-defined types that are constructed from entities of intrinsic data types
(see “Intrinsic data types” on page 107) or entities of previously defined derived types. For
example, the following is a definition of a derived type for manipulating coordinates consisting
of two real numbers:
TYPE coord
REAL :: x,y
END TYPE coord
x and y are the components of the derived type coord.
The next statement declares two variables (a and b) of the derived type coord:
TYPE(coord) :: a, b
The next statement copies the values of a to b, as in any assignment statement:
a = b
The components of a and b are referenced as a%x, a%y, b%x, and b%y. By using the defined
operation facility of Fortran 90, it is possible to extend the standard operators to work with
derived types. For example, if the + and = operators were re-defined to operate on derived type
operands, the following statement
a = a + b
would be equivalent to
a%x = a%x + b%x; a%y = a%y + b%y
The following sections describe:
• The syntax of defining a derived type
• Sequence types
• Structure constructors
• Referencing a structure component
• Alignment of derived type objects
The last section provides an example program that illustrates different features of derived
types.
Defining a derived type
The format for defining a derived type is: