Modules and Constants
Named Constants
Parameter
parameter defines a compile-time constant. Modules often expose constants this way.
Program
Play the program to subtract from a constant defined in a module.
named_constants.f90
Replay: real traced execution (multi-file project)
module constants_mod
implicit none
integer, parameter :: max_users = 100
end module constants_mod
program named_constants_demo
use constants_mod
implicit none
integer :: free_slots, used
used = 7
free_slots = max_users - used
print '(I0)', free_slots
end program named_constants_demo
module constants_mod
implicit none
integer, parameter :: max_users = 100
end module constants_mod
program named_constants_demo
use constants_mod
implicit none
integer :: free_slots, used
used = 12
free_slots = max_users - used
print '(I0)', free_slots
end program named_constants_demo
module constants_mod
implicit none
integer, parameter :: max_users = 100
end module constants_mod
program named_constants_demo
use constants_mod
implicit none
integer :: free_slots, used
used = 30
free_slots = max_users - used
print '(I0)', free_slots
end program named_constants_demo
used ← 7
9integer :: free_slots, used10used = 711free_slots = max_users - usedvalues this step7usedfree_slots ← 93
10used = 711free_slots = max_users - used12print '(I0)', free_slotsvalues this step93free_slots100max_users7usedprint '(I0)', free_slots
11 free_slots = max_users - used12 print '(I0)', free_slots13end program named_constants_demooutput93values this step93free_slots
used ← 12
9integer :: free_slots, used10used = 1211free_slots = max_users - usedvalues this step12usedfree_slots ← 88
10used = 1211free_slots = max_users - used12print '(I0)', free_slotsvalues this step88free_slots100max_users12usedprint '(I0)', free_slots
11 free_slots = max_users - used12 print '(I0)', free_slots13end program named_constants_demooutput88values this step88free_slots
used ← 30
9integer :: free_slots, used10used = 3011free_slots = max_users - usedvalues this step30usedfree_slots ← 70
10used = 3011free_slots = max_users - used12print '(I0)', free_slotsvalues this step70free_slots100max_users30usedprint '(I0)', free_slots
11 free_slots = max_users - used12 print '(I0)', free_slots13end program named_constants_demooutput70values this step70free_slots
Follow the Constant
constants_moddefinesmax_users = 100.usedstarts at7.free_slots = max_users - used.- The default calculation is
100 - 7. - The program prints
93. | used | max_users | free_slots | | --- | --- | --- | | 7 | 100 | 93 | | 12 | 100 | 88 | | 30 | 100 | 70 |
parameter
`integer, parameter :: name = value` defines a constant.
module constant
Modules can publish constants the same way they publish procedures.
immutable
A `parameter` cannot be reassigned at run time.
Exercise: named_constants.f90
Reproduce the printed value 93, then use the pinned used values 12 and 30 to predict each free slot count.