Modules and Constants
Modules
Share Procedures
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
call ← area(4, 3)
14integer :: result_15result_ = area(4, 3)16print '(I0)', result_values this steparea(4, 3)calla ← 12
6 integer :: a7 a = width * height8end function areavalues this step12a4width3heightresult_ ← 12
14integer :: result_15result_ = area(4, 3)16print '(I0)', result_values this step12result_print '(I0)', result_
15 result_ = area(4, 3)16 print '(I0)', result_17end program module_use_demooutput12values this step12result_
Follow the Module
module geomdefines anareafunction.area(width, height)multiplieswidth * height.- The program says
use geomso it can callarea. result_ = area(4, 3)becomes12.- 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.