A module collects procedures so other units can use them. The contains block holds procedure bodies.

Program

Play the program to call an area function from a module.

module_use.f90
Replay: real traced execution (multi-file project)
module geom
    implicit none
contains
    function area(width, height) result(a)
        integer, intent(in) :: width, height
        integer :: a
        a = width * height
    end function area
end module geom

program module_use_demo
    use geom
    implicit none
    integer :: result_
    result_ = area(4, 3)
    print '(I0)', result_
end program module_use_demo
  1. call ← area(4, 3)

    14integer :: result_15result_ = area(4, 3)16print '(I0)', result_
    values this steparea(4, 3)call
  2. a ← 12

    6    integer :: a7    a = width * height8end function area
    values this step12a4width3height
  3. result_ ← 12

    14integer :: result_15result_ = area(4, 3)16print '(I0)', result_
    values this step12result_
  4. print '(I0)', result_

    15    result_ = area(4, 3)16    print '(I0)', result_17end program module_use_demo
    output12
    values this step12result_

Follow the Module

  1. module geom defines an area function.
  2. area(width, height) multiplies width * height.
  3. The program says use geom so it can call area.
  4. result_ = area(4, 3) becomes 12.
  5. The program prints 12. | call | width | height | result | | --- | --- | --- | --- | | area(4, 3) | 4 | 3 | 12 |
module `module ... end module` defines a reusable unit.
use `use geom` imports the module's public names.
contains `contains` separates the module body from its procedures.

Exercise: module_use.f90

Reproduce the printed value 12, then identify the two arguments passed to area.