Multi-File Program Structure
Procedure Partition
Keeping Work Behind Calls
A module subroutine can own the calculation while the caller passes data in and receives a result out.
Program
Play the program to choose a quantity and let the module procedure calculate the total.
procedure_partition.f90
Replay: real traced execution (multi-file project)
module invoice_mod
implicit none
contains
subroutine compute_total(quantity, unit_price, total)
integer, intent(in) :: quantity
integer, intent(in) :: unit_price
integer, intent(out) :: total
total = quantity * unit_price
end subroutine compute_total
end module invoice_mod
program procedure_partition_demo
use invoice_mod, only: compute_total
implicit none
integer :: quantity
integer :: total
quantity = 4
call compute_total(quantity, 7, total)
print '(I0, 1X, I0)', quantity, total
end program procedure_partition_demo
module invoice_mod
implicit none
contains
subroutine compute_total(quantity, unit_price, total)
integer, intent(in) :: quantity
integer, intent(in) :: unit_price
integer, intent(out) :: total
total = quantity * unit_price
end subroutine compute_total
end module invoice_mod
program procedure_partition_demo
use invoice_mod, only: compute_total
implicit none
integer :: quantity
integer :: total
quantity = 1
call compute_total(quantity, 7, total)
print '(I0, 1X, I0)', quantity, total
end program procedure_partition_demo
module invoice_mod
implicit none
contains
subroutine compute_total(quantity, unit_price, total)
integer, intent(in) :: quantity
integer, intent(in) :: unit_price
integer, intent(out) :: total
total = quantity * unit_price
end subroutine compute_total
end module invoice_mod
program procedure_partition_demo
use invoice_mod, only: compute_total
implicit none
integer :: quantity
integer :: total
quantity = 6
call compute_total(quantity, 7, total)
print '(I0, 1X, I0)', quantity, total
end program procedure_partition_demo
quantity ← 4
18quantity = 419call compute_total(quantity, 7, total)values this step4quantitytotal ← 28
18quantity = 419call compute_total(quantity, 7, total)20print '(I0, 1X, I0)', quantity, totalvalues this step28total4quantity7unit_priceprint '(I0, 1X, I0)', quantity, total
19 call compute_total(quantity, 7, total)20 print '(I0, 1X, I0)', quantity, total21end program procedure_partition_demooutput4 28values this step4quantity28total
quantity ← 1
18quantity = 119call compute_total(quantity, 7, total)values this step1quantitytotal ← 7
18quantity = 119call compute_total(quantity, 7, total)20print '(I0, 1X, I0)', quantity, totalvalues this step7total1quantity7unit_priceprint '(I0, 1X, I0)', quantity, total
19 call compute_total(quantity, 7, total)20 print '(I0, 1X, I0)', quantity, total21end program procedure_partition_demooutput1 7values this step1quantity7total
quantity ← 6
18quantity = 619call compute_total(quantity, 7, total)values this step6quantitytotal ← 42
18quantity = 619call compute_total(quantity, 7, total)20print '(I0, 1X, I0)', quantity, totalvalues this step42total6quantity7unit_priceprint '(I0, 1X, I0)', quantity, total
19 call compute_total(quantity, 7, total)20 print '(I0, 1X, I0)', quantity, total21end program procedure_partition_demooutput6 42values this step6quantity42total
subroutine
`compute_total` performs work through arguments instead of returning a value.
intent
`intent(in)` and `intent(out)` document data direction across the boundary.
partition
The caller stays small because the calculation lives behind a module call.