13

According to the Fortran standard:

The INTENT (OUT) attribute for a nonpointer dummy argument specifies that the dummy argument becomes undefined on invocation of the procedure

However this simple code gives me 5 as output, so it seems the argument didn't become undefined at the start of the procedure (in this case a subroutine).

subroutine useless(a)
  integer, intent(out) :: a
  print *,a
end subroutine useless

program test
  integer :: n=5
  call useless(n)
end program test

What am I getting wrong? It seems that intent(inout) and intent(out) are the same.

Ross
  • 2,130
  • 14
  • 25
Zero
  • 262
  • 3
  • 8
  • 1
    Interestingly, both gfortran and ifort print 5, although ifort warns that `A dummy argument with an explicit INTENT(OUT) declaration is not given an explicit value`. – Raul Laasner Apr 20 '15 at 07:32
  • @Ross you are probably right with the re-tag, but there so many similar questions out there... Probably better wait until you can edit without review if you want to do many such edits. I often just leave the [tag:fortran90] there even when adding the generic tag to be sure not to change OP's intents. – Vladimir F Героям слава Jul 16 '15 at 21:06

1 Answers1

14

intent(inout) and intent(out) are certainly not the same. You have noted why, although you don't draw the correct conclusion. On entering the subroutine useless a is undefined, rather than defined.

Having a variable "undefined" means that you cannot rely on a specific behaviour when you reference it. You observed that the variable a had a value 5 but this does not mean that the only value you could observe is 5. In particular "undefined" does not mean "takes a specific value like NaN".

Your code is not standard conforming because of this reference to an undefined variable. See Fortran 2008 6.2 (similar meaning will be somewhere in Fortran 90 as initially tagged). Of particular note is that the compiler doesn't have to point out your mistake.

With intent(inout) the variable a would be defined when referenced and it will be guaranteed to have the value 5 (for a conforming processor).

More widely, there are other differences between the two intent attributes and this "coincidental" appearance of the similarity of the definition of the variable a could be more troublesome.

Allocatable arrays and objects with deferred type parameters, for example, are deallocated; derived types become undefined (and any allocatable components deallocated) and components with default initialization are "re-initalized"; pointers have their association status become undefined.

All of these latter things have potential for very awkward results, much more so than with a scalar integer, if they are referenced without being defined first.

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • Are there any examples showing different outputs using `intent(inout)` and `intent(out)`? –  Nov 06 '19 at 15:58
  • 1
    @Jack, this question is about an _invalid_ program. If you want a _valid_ program with a difference when using `intent(inout)` rather than `intent(out)` then you have to try a little harder: with an `intent(out)` dummy a reference to it in the procedure must follow a definition in that procedure - which would override the definition of the comparable `intent(inout)` dummy. However, yes, it is possible for a valid program to give different results simply changing `intent(inout)` to `intent(out)` or _vice versa_. A comment can't show that so if you're keen enough, please ask a new question. – francescalus Nov 06 '19 at 16:14
  • Thanks for your reply! I was searching around to see if something similar had been asked/done. It seems not. I will post a question soon. –  Nov 06 '19 at 16:42
  • A follow-up question has been posted: https://stackoverflow.com/q/58735057 –  Nov 06 '19 at 16:56