INTENT

Contents
Intent in
Intent out
Intent inout
Compiler checks

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)

Next: OPTIONAL and PRESENT

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: