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.

used
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
  1. used ← 7

    9integer :: free_slots, used10used = 711free_slots = max_users - used
    values this step7used
  2. free_slots ← 93

    10used = 711free_slots = max_users - used12print '(I0)', free_slots
    values this step93free_slots100max_users7used
  3. print '(I0)', free_slots

    11    free_slots = max_users - used12    print '(I0)', free_slots13end program named_constants_demo
    output93
    values this step93free_slots
  1. used ← 12

    9integer :: free_slots, used10used = 1211free_slots = max_users - used
    values this step12used
  2. free_slots ← 88

    10used = 1211free_slots = max_users - used12print '(I0)', free_slots
    values this step88free_slots100max_users12used
  3. print '(I0)', free_slots

    11    free_slots = max_users - used12    print '(I0)', free_slots13end program named_constants_demo
    output88
    values this step88free_slots
  1. used ← 30

    9integer :: free_slots, used10used = 3011free_slots = max_users - used
    values this step30used
  2. free_slots ← 70

    10used = 3011free_slots = max_users - used12print '(I0)', free_slots
    values this step70free_slots100max_users30used
  3. print '(I0)', free_slots

    11    free_slots = max_users - used12    print '(I0)', free_slots13end program named_constants_demo
    output70
    values this step70free_slots

Follow the Constant

  1. constants_mod defines max_users = 100.
  2. used starts at 7.
  3. free_slots = max_users - used.
  4. The default calculation is 100 - 7.
  5. 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.