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
  1. a ← 10

    3integer :: a, b, sum_, diff4a = 105b = 3
    values this step10a
  2. b ← 3

    4a = 105b = 36call summarize(a, b, sum_, diff)
    values this step3b
  3. call ← summarize(a, b, sum_, diff)

    5b = 36call summarize(a, b, sum_, diff)7print '(I0, A, I0)', sum_, " ", diff
    values this stepsummarize(a, b, sum_, diff)call10a3b
  4. s ← 13

    11integer, intent(out) :: s, d12s = x + y13d = x - y
    values this step13s10x3y
  5. d ← 7

    12    s = x + y13    d = x - y14end subroutine summarize
    values this step7d10x3y
  6. print '(I0, A, I0)', sum_, " ", diff

    6    call summarize(a, b, sum_, diff)7    print '(I0, A, I0)', sum_, " ", diff8contains
    output13 7
    values this step13sum_7diff

Follow the Outputs

  1. a starts at 10.
  2. b starts at 3.
  3. call summarize(a, b, sum_, diff) sends both values into the subroutine.
  4. The subroutine sets sum_ to 10 + 3, which is 13.
  5. It sets diff to 10 - 3, which is 7, and the program prints 13 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.