Modules are useful because all variables within them—which can include derived datatypes—can be accessed (unless designated as private, see below) by any subprogram which references the module with a use statement. In addition, at compilation time, calls to module subprograms are checked for compatibility with their declared interfaces, which is helpful in preventing bugs.

Note that many compilers require that the definition of a module precede its use. When a source file for a module is compiled, a .mod file is created (Example: gfortran -c my_module.f90 creates a file my_module.mod). This file is needed for the compilation of all programs and subprograms that use the module. Consequently the modules have to be compiled before everything else.

An example of a module, my_module.f90, and a main program using it, demo.f90, is given below. The module and program can also be combined into a single source file (in the order shown) and compiled that way. The main program as written will not work because it attempts to call a subroutine that is private to the module.

module my_module
type person                        ! derived datatype
  character(len=10) :: name
  real              :: age
  integer           :: id
end type person
type (person), dimension(10) :: we ! array of structures

private             :: reset  ! The subroutine reset (defined
                              ! below after the "contains"
                              ! statement) is made private,
                              ! i.e. it is only accessible from
                              ! within the module

contains                      ! Subroutines and functions are
                              ! placed after the contains
                              ! statement

subroutine print(p)           ! subroutine contained in the
                              ! module
type(person)     :: p
write (*,'(a,a10,1x,f4.1,1x,i8)') 'Name, age, id : ', &
             p%name, p%age, p%id
end subroutine print

subroutine print_and_reset(p)
type(person)     :: p
write (*,'(a,a10,1x,f4.1,1x,i8)') 'Name, age, id : ', &
             p%name, p%age, p%id
call reset(p)
end subroutine print_and_reset

subroutine reset(p)           ! Private subroutine only
type(person)     :: p         ! accessible from within module
p%name = 'undef'
p%age  = -1.
p%id   = 0
end subroutine reset

end module my_module


program demo
use my_module
type(person) :: you

you%name ='John Doe'
you%age  = 25.
you%id   = 1234

call print(you)            ! Print you (John Doe, 25.0, 1234)
call print_and_reset(you)  ! Print, reset (John Doe, 25.0, 1234)
call print(you)            ! Print you (undef, -1.0, 0)
call reset(p)              ! Subroutine reset is not directly
                           ! accessible from the main program

end program demo
Fortran
 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)