Fortran DISCUSSION

Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

Started by dattakakade Fortran intent attributedummy argumentsexplicit interfaceintent(out) allocatablemodifying constants
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

dattakakade Fortran Forum
#1

Older routines in the code I maintain declare dummy arguments with no intent at all, newer ones use intent(in), intent(out) or intent(inout). Everything compiles either way. One old routine even modifies an argument that a caller passes as the literal 5, and that program crashes on one machine and runs on another.

Is an argument without intent the same as intent(inout)? What does each intent actually promise, and is it worth adding them to existing code?

Community replies 5

Re: Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

#2

No intent is not the same as intent(inout). With intent(inout) the actual argument must be definable, that is a variable, so passing 5 or an expression such as n+1 is an error. With no intent the compiler assumes nothing: you may pass a constant or an expression, provided the routine does not in fact assign to that dummy.

If it does assign, the program is non-conforming and nobody is required to tell you. That is your crash: one system keeps the constant 5 in read-only memory and faults; another lets the routine overwrite it, after which later uses of that constant may see the new value.

Re: Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

#3

What each one promises: intent(in) means the routine will not change the argument, and the compiler rejects any assignment to it, or passing it on to another routine's intent(out) or intent(inout) dummy. intent(out) means the value on entry is not used; the argument becomes undefined when the routine starts and the routine is expected to assign it. intent(inout) means the value comes in and may be updated.

The important trap is intent(out): if the routine returns without assigning it, the caller's variable is undefined. It is not guaranteed to keep the value it had before the call.

Re: Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

#4

intent(out) has real effects beyond documentation. An allocatable dummy with intent(out) is automatically deallocated on entry, and a derived-type dummy has its default component initialisation applied again. So a routine that is meant to append to an existing allocatable array, or to update a few fields of a structure, must use intent(inout); marking it intent(out) wipes the data before your first statement runs.

For pointer dummies, the intent refers to the pointer association, not to the value of the target.

Re: Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

#5

The compiler can only enforce intents at the call site when it sees the routine's interface, which means the routine lives in a module, is an internal procedure, or has an interface block. For old-style external routines compiled separately, the caller knows nothing and the literal 5 goes through unchecked.

Moving routines into modules is therefore the step that gives the intents their teeth: with intent(inout) declared and the interface visible, call bump(5) becomes a compile-time error instead of a crash.

Re: Fortran intent(in), intent(out), intent(inout): what changes if I leave the intent off?

#6

It is worth adding them, and it can be done incrementally. Start with intent(in) on everything the routine only reads; the compiler then finds the places where that assumption is wrong. Declared intents are also a prerequisite for marking procedures pure, which in turn is what allows them to be used safely in do concurrent and similar constructs.

If a routine needs a private scratch copy of a scalar argument, the value attribute gives C-like pass by value: changes stay local to the routine.

TEP COMMUNITY