When a subroutine is entered, any dummy argument that has the attribute intent(out) becomes undefined as soon as the subroutine is entered. Even if the corresponding actual argument is well defined before the call, the corresponding dummy argument becomes undefined after the subroutine is entered. The decade old US Navy/NASA program HWM14 contains a troublesome bug that may be attributed to not knowing this behaviour. It took considerable effort to track this down, and in this case FTN95, despite its well-deserved reputation for detecting errors, failed to pinpoint the error. I created a short, if silly, test code to point out the issue. The code:
program demo
implicit none
integer :: iw(2)
iw(1)=2; iw(2)=3
call sub(iw)
print *,iw
end program
subroutine sub(jw)
implicit none
integer,intent(out) :: jw(2) ! wrong intent, makes jw undefined
jw(2)=2*jw(1)
jw(1)=jw(1)+jw(2)
return
end subroutine
Compile and link with /undef and run. No error messages!
Lahey Fortran 7.1 catches the bug:
S:\>demo
jwe0323i-w line 9 The variable (jw(1)) has an undefined value.
error occurs at _sub_ line 9 loc 0040117c offset 00000069
_sub_ at loc 00401113 called from loc 004010ba in _MAIN__ line 4
_MAIN__ at loc 00401000 called from o.s
The NAG compiler, with its C=undefined -gline options, also catches the error:
Runtime Error: demo.f90, line 12: Reference to undefined variable JW(1)
Program terminated by fatal error
demo.f90, line 12: Error occurred in SUB
demo.f90, line 5: Called by DEMO