Fortran 90: Derived Data Types

The Fortran 90 derived data type is similar to C structures and also has some similarities with C++ classes. The syntax for declaring a derived type, is
type mytype
   integer:: i
   real*8 :: a(3)
end type mytype
To create a variable of type mytype, use
type (mytype) var
An array of mytype can also be created.
type (mytype) stuff(3)
Elements of derived types are accessed with the "%" operator. For instance,
var%i = 3
var%a(1) = 4.0d0
stuff(1)%a(2) = 8.0d0
The real power of derived types comes with the ability to choose bewteen functions (or subroutines) based on the type of their arguments. Different functions can be called by the same name, depending on whether the argument type is real, integer,or even a derived type. Intrinsic Fortran routines have always had this ability, the simplest example being choosing between single and double precision versions of the function. Now it can be extended to user's routines and defined data types as well. See the example in the section on intefaces for subroutines .

The compiler is free to store the constitutients of a derived type how it chooses. To force the derived type to be stored contiguously, use the sequence keyword. For example,

type mytype
   sequence
   integer:: i
   real*8 :: a(3)
end type mytype
The IBM compiler seems to require this keyword if a derived type is the argument of a subroutine or function.
Return to Fortran 90 index