Dear all,
a pure procedure internal to a subroutine should not be able to modify the variables of its parent subroutine (apart, of course, from those passed to it as arguments with the intent (in)out). This is what gfortran signals in the example below, but not ifort or nagfor. Why?
module foo_m
implicit none
contains
subroutine foo ( a, b )
integer, intent(in out) :: a, b
b = 0
call sbar ( a )
b = fbar ( a )
contains
pure subroutine sbar ( a )
integer, intent(out) :: a
a = b
b = 1 ! <- shouldn't the compiler report an error?
! (gfortran does, but not nagfor or ifort)
end subroutine sbar
pure function fbar ( a ) result( r )
integer, intent(in) :: a
integer :: r
r = a+b
b = 2 ! <- same here
end function fbar
end subroutine foo
end module foo_m
It seems logical to me that gfortran gives:
gfortran foo_m.f90 -c
foo_m.f90:23:9:
23 | b = 2 ! <- same here
| 1
Error: Variable 'b' cannot appear in a variable definition context (assignment) at (1) in PURE procedure
foo_m.f90:15:9:
15 | b = 1 ! <- shouldn't the compiler report an error?
| 1
Error: Variable 'b' cannot appear in a variable definition context (assignment) at (1) in PURE procedure
But why don’t ifort and nagfor?