Derived types

Yutaka Masuda

February 2020

Back to index.html.

Derived types

Use of derived types

Definition

There is a case where several variables should be collectively managed as a unit. For example, considering a manage an animal’s profile, you may need a name, breed, and age. A standard program uses 3 variables.

program derived
   implicit none
   character(len=50) :: name
   character(len=50) :: breed
   integer :: age 
   ...  
end program derived

It is not intuitive that all these variables are collective. Instead, we can get several variables together into a single “box”, and treat it as a new variable. The “box” is called derived type, which defines what kind of variables it has. In Fortran, the type statement defines a derived type, a composite of many member variables.

program derived
   implicit none
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal
   ...  
end program derived

The statements type and end type define a template of the derived type. This new type works like an intrinsic type (e.g., integer and real). So, when you use the derived type, you have to define a variable with this new type.

program derived
   implicit none

   ! It only defines a template.
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal

   ! It declares a variable that you can use.
   type(animal) :: x
   ...  
end program derived

A derived type is sometimes called a structure, which is common in C or other programming languages.

Access to member variables

A variable in a derived type is a standard variable – You can change and refer to it. To access a member variable (component variable) in a derived type, you have to use % as follows.

program derived
   implicit none
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal
   type(animal) :: x

   x%name = "John"
   x%breed = "Golden Retriever"
   x%age = 2

   print *,"age=",x%age
end program derived

You can print all the member variables.

print *,x

To assign several values to the member variables at a time, use the type-name as if it was a function.

program derived
   implicit none
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal
   type(animal) :: x

   x = animal("John", "Golden Retriever", 2)

   print *,"age=",x%age
end program derived

Array of a derived type

You can define an array of a derived type.

program derived
   implicit none
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal
   type(animal) :: x(2)

   x(1) = animal("John", "Golden Retriever", 2)
   x(2) = animal("Peter", "Chihuahua", 4)

   x(1)%age = x(1)%age + 1
   print *,x(2)
end program derived

The array can be allocatable.

program derived
   implicit none
   type animal
      character(len=50) :: name
      character(len=50) :: breed
      integer :: age 
   end type animal
   type(animal),allocatable :: x(:)

   allocate(x(2))
   ...
   deallocate(x)
end program derived

Back to index.html.