Procedures
Intent
In, Out, Inout
intent declares how an argument is used. in reads only, out writes only, inout both.
Program
Play the program to compute sum and difference through one subroutine.
intents.f90
Replay: real traced execution (multi-file project)
program intents_demo
implicit none
integer :: a, b, sum_, diff
a = 10
b = 3
call summarize(a, b, sum_, diff)
print '(I0, A, I0)', sum_, " ", diff
contains
subroutine summarize(x, y, s, d)
integer, intent(in) :: x, y
integer, intent(out) :: s, d
s = x + y
d = x - y
end subroutine summarize
end program intents_demo
a ← 10
3integer :: a, b, sum_, diff4a = 105b = 3values this step10ab ← 3
4a = 105b = 36call summarize(a, b, sum_, diff)values this step3bcall ← summarize(a, b, sum_, diff)
5b = 36call summarize(a, b, sum_, diff)7print '(I0, A, I0)', sum_, " ", diffvalues this stepsummarize(a, b, sum_, diff)call10a3bs ← 13
11integer, intent(out) :: s, d12s = x + y13d = x - yvalues this step13s10x3yd ← 7
12 s = x + y13 d = x - y14end subroutine summarizevalues this step7d10x3yprint '(I0, A, I0)', sum_, " ", diff
6 call summarize(a, b, sum_, diff)7 print '(I0, A, I0)', sum_, " ", diff8containsoutput13 7values this step13sum_7diff
Follow the Outputs
astarts at10.bstarts at3.call summarize(a, b, sum_, diff)sends both values into the subroutine.- The subroutine sets
sum_to10 + 3, which is13. - It sets
diffto10 - 3, which is7, and the program prints13 7. | argument or output | value | | --- | --- | |a| 10 | |b| 3 | |sum_| 13 | |diff| 7 |
intent(in)
Read-only argument; the caller's value is unchanged.
intent(out)
Output-only argument; the callee assigns it.
multiple outputs
Use `intent(out)` arguments to return more than one value from a subroutine.
Exercise: intents.f90
Reproduce the output 13 7, then change b to 4 and predict the new sum and difference before running it.