10. Derived data types

A useful feature in Fortran 90 is the ability to define derived data types. This feature is similar to the structure feature of the C programming language. The manner in which one defines and uses a derived data type is best illustrated through example:

   TYPE Point
      REAL :: X, Y
   END TYPE Point

   TYPE Circle
      TYPE (Point) :: Center
      REAL :: Radius
   END TYPE Circle

   TYPE (Circle) :: C

This declares the variable C to be of type Circle. C formally has two components. The first component, Circle, is itself a derived data type consisting of two real components called X and Y. The second component of C is a variable named Radius of type real.

The variable C may be assigned a value in either of these ways:

C = Circle(Point(0., 0.), 1.)

or, equivalently

C%Center%X = 0.
C%Center%Y = 0.
C%Radius = 1.

In both cases, the "circle" C was assigned the center point (0., 0.) and given the radius 1. (the unit circle).

There are many practical uses for derived data types. And, these data types function just like any other variable in a program unit, namely, they may be initialized, modified, and even passed to subprograms as parameters.

Copyright 1996-7 by Stanford University. All rights reserved.