A module can hold reusable procedures while the main program focuses on choosing inputs and printing results.

Program

Play the program to choose a factor and call a procedure through the module API.

factor
module_api_call.f90
Replay: real traced execution (multi-file project)
module scale_api
    implicit none
contains
    function scaled_count(count, factor) result(total)
        integer, intent(in) :: count
        integer, intent(in) :: factor
        integer :: total
        total = count * factor
    end function scaled_count
end module scale_api

program module_api_call_demo
    use scale_api, only: scaled_count
    implicit none
    integer :: factor
    integer :: total

    factor = 3
    total = scaled_count(8, factor)
    print '(I0, 1X, I0)', factor, total
end program module_api_call_demo
module scale_api
    implicit none
contains
    function scaled_count(count, factor) result(total)
        integer, intent(in) :: count
        integer, intent(in) :: factor
        integer :: total
        total = count * factor
    end function scaled_count
end module scale_api

program module_api_call_demo
    use scale_api, only: scaled_count
    implicit none
    integer :: factor
    integer :: total

    factor = 2
    total = scaled_count(8, factor)
    print '(I0, 1X, I0)', factor, total
end program module_api_call_demo
module scale_api
    implicit none
contains
    function scaled_count(count, factor) result(total)
        integer, intent(in) :: count
        integer, intent(in) :: factor
        integer :: total
        total = count * factor
    end function scaled_count
end module scale_api

program module_api_call_demo
    use scale_api, only: scaled_count
    implicit none
    integer :: factor
    integer :: total

    factor = 5
    total = scaled_count(8, factor)
    print '(I0, 1X, I0)', factor, total
end program module_api_call_demo
  1. factor ← 3

    18factor = 319total = scaled_count(8, factor)
    values this step3factor
  2. total ← 24

    18factor = 319total = scaled_count(8, factor)20print '(I0, 1X, I0)', factor, total
    values this step24total3factor
  3. print '(I0, 1X, I0)', factor, total

    19    total = scaled_count(8, factor)20    print '(I0, 1X, I0)', factor, total21end program module_api_call_demo
    output3 24
    values this step3factor24total
  1. factor ← 2

    18factor = 219total = scaled_count(8, factor)
    values this step2factor
  2. total ← 16

    18factor = 219total = scaled_count(8, factor)20print '(I0, 1X, I0)', factor, total
    values this step16total2factor
  3. print '(I0, 1X, I0)', factor, total

    19    total = scaled_count(8, factor)20    print '(I0, 1X, I0)', factor, total21end program module_api_call_demo
    output2 16
    values this step2factor16total
  1. factor ← 5

    18factor = 519total = scaled_count(8, factor)
    values this step5factor
  2. total ← 40

    18factor = 519total = scaled_count(8, factor)20print '(I0, 1X, I0)', factor, total
    values this step40total5factor
  3. print '(I0, 1X, I0)', factor, total

    19    total = scaled_count(8, factor)20    print '(I0, 1X, I0)', factor, total21end program module_api_call_demo
    output5 40
    values this step5factor40total
module A module groups reusable procedures behind a named API.
use only `use scale_api, only: scaled_count` imports one public procedure.
main program The program chooses inputs and delegates the calculation.