Skip to main content

9. Derived data types

Derived types in f90 are also called structures. A structure in f90 allows the user to create an object that can collect together objects of differing type and size. An example of a structure is a database entry. Consider a database for tennis players, where the entries are name (character type), age (integer type), prizemoney (real type), number of titles (integer type). To make use of this database as a structure (called tennis) in a f90 program, it first must be defined:

type tennis 
character (len=100) :: name
integer :: age
real :: prize
integer :: titles
end type tennis

This definition should come at the top of the program - ideally, you should encapsulate this definition into a module. You can now use this stucture definition as you would any other type definition. Therefore, you can use this "user-declared" type to declare variables. The only difference between declaring a variable based on normal types and declaring a variable based on a structure, is that in the latter case you must enclose your type name in parentheses, and precede this with the word "type". Following the above example, we could define a variable player in the following way:

type (tennis) :: player

This would allow us to assign the details of one tennis player. Assignment of values for the various components of the variable player can be done in the following way:

player%name="Andre Agassi"

Alternatively, you could set up the variable player by typing

player = tennis ("Andre Agassi",35,5000000.99,65)

Note that in the latter case, your order of entries must match the order given in the declaration. You can also set up arrays of your derived data type (in this case, turn player into an array, each holding the details of a different tennis player).