When do I have to use CONTAINS in a module?

Viewed 135

I have the following module defined:

module data_model_2

        implicit none

        ! -------------------
        ! TYPE DEFINITION
        ! -------------------
        type :: type1_record
                integer                 :: month
                integer                 :: day
                integer                 :: year
                integer                 :: hour
                integer                 :: minute
                integer                 :: second
                integer                 :: value1
                integer                 :: value2
        end type
        type :: timestamp_record
                integer                 :: year
                integer                 :: month
                integer                 :: day
                integer                 :: hour
                integer                 :: minute
                integer                 :: second
        end type

end module

I have seen that modules shall use the clause contains. Is the clause needed when the module contains only derived types definition?

If so, where shall it be included, after or before the implicit none?

If the module would include also functions or procedures, shall the derived types definitions be before or after contains clause?

I am interested in knowing how this works in Fortran 95 and Fortran 03.

1 Answers

The contains statement says that what follows are internal subprograms or module subprograms. Only those things may appear between contains and the end of the module.

Before Fortran 2008 it was not allowed to have an "empty contains" section: if you have no module subprograms in Fortran 90, 95, 2003 then you may not have contains. In Fortran 2008+ you may have it, although it is not required, without them.

contains is essentially the last thing allowed in a module: all module-scope definitions which aren't internal/module subprograms must be before. An implicit statement must be very early in the module scope: only the module statement, use and import (in those supported rare cases) may preceed it.

Related