4
votes

This question is connected to the problem: how to detect violation of intent(in) inside subprograms. But I haven't found the answer in the related question Enforce intent(in) declared variables in Fortran as constant also in called subroutines/functions.

A variable which is declared as intent(in) can be modified by another subprogram/function with omitted intent declaration.

For example:

module test
  implicit none
  contains

  subroutine fun1(x)
    real(8), intent(in)::x
    call fun2(x)
  end subroutine

  subroutine fun2(x)
    real(8) :: x
    x = 10
  end subroutine
end module

This code can be compiled without any errors/warnings by gfortran and ifort. So my questions are:

  1. Is it possible to forbid omitting intent declaration?
  2. Is it possible to force a Fortran compiler to interpret omitted intent as intent(inout)?
2

2 Answers

4
votes

Both answers are NO. Unspecified intent is fundamentally different from all other intents. It is different from intent(inout), because you can pass a nondefinable expression to a subroutine with unspecified intent.

Also in many contexts it is not allowed to specify intent at all (procedure arguments, pointers in Fortran 95,...)

If you want to require specifying of intent, you may define your subroutine as pure but it does much more than that. But it may be the right thing for you. It forbids any side-effects.

-2
votes

I think you should get a compile error due to the automatically defined interface. I would expect the same with a wrong dimension for example (I switched the fun2 dummy argument x to z that I think demonstrates more clearly my point).

module test
  implicit none
  contains

  subroutine fun1(x)
    real(8), intent(in)::x
    call fun2(x)
  end subroutine

  subroutine fun2(z)
    real(3) :: z
    z = 10
  end subroutine
end module