typestar

Subroutines and intent in Fortran

A causeway verdict via subroutine: intent tells the compiler which way data flows.

program subroutine_intent
    implicit none
    real :: feet = 11.2
    character(len=24) :: advice

    call causeway_call(feet, advice)
    print *, feet, " ft -- ", trim(advice)

contains
    ! intent tells the compiler which way each argument flows.
    subroutine causeway_call(tide_ft, verdict)
        real, intent(in) :: tide_ft
        character(len=*), intent(out) :: verdict
        if (tide_ft > 9.5) then
            verdict = "stay off the causeway"
        else
            verdict = "safe to cross"
        end if
    end subroutine causeway_call
end program subroutine_intent

How it works

  1. call causeway_call(feet, advice) invokes; contains holds the definition below.
  2. intent(in) makes the tide reading read-only inside; the compiler enforces it.
  3. intent(out) marks the verdict as the result path; len=* takes the caller's length.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
496
Tokens
87
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 80 seconds.

Type this snippet

Step 1 of 3 in Procedures, step 13 of 27 in Language basics.

← Previous Next →