INTENT

Intent in

Intent in marks a read only input: the procedure may look at the value but may never change it.

Use it for every argument that flows one way inward, which is the most common case by far.

program intentin implicit none call show_twice(5) contains subroutine show_twice(x) implicit none integer, intent(in) :: x print *, x * 2 end subroutine show_twice end program intentin

./intentin

10

Intent out

Intent out marks a write only result: the procedure must assign it, and its entry value is undefined.

Use it for every argument that flows one way outward, such as status codes and computed answers.

program intentout implicit none integer :: v call make_answer(v) print *, v contains subroutine make_answer(x) implicit none integer, intent(out) :: x x = 42 end subroutine make_answer end program intentout

./intentout

42

Intent inout

Intent inout marks a two way argument: the procedure reads the entry value and writes back an update.

Use it for counters, accumulators, and anything that is transformed in place rather than replaced.

program intentinout implicit none integer :: n n = 1 call bump(n) call bump(n) print *, n contains subroutine bump(x) implicit none integer, intent(inout) :: x x = x + 1 end subroutine bump end program intentinout

./intentinout

3

Compiler checks

The compiler enforces intent: assigning to an intent in dummy or passing a constant to an intent out dummy stops the build.

Read the error, fix the intent or the call, and enjoy a whole class of bugs that can never reach runtime.

program badintent implicit none call set_zero(7) contains subroutine set_zero(x) implicit none integer, intent(out) :: x x = 0 end subroutine set_zero end program badintent

gfortran bad.f90 -o bad

Error: Non-variable expression in variable definition context (actual argument to INTENT = OUT/INOUT) at (1)

The code on this page was run with gfortran 15.2.0 on Alpine Linux 3.24 (x86_64) in a Docker container.

Article author: Arthur Isaev