Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Variables in a contained subroutine

OP1
New Contributor III
407 Views
Assume that I have a module M, which CONTAINs a subroutine S. Assume that M declares a variable V; Assume that S also declared a variable V.

In this case, Fortran dictates that the local definition of V in S takes precedence over the definition of V in M. This is handled very well by the compiler of course; not so much by the human brain :) as it can get confusing.

I have two questions regarding this:

1.Is there a way to have the compiler emit a warning when a local declaration (in a contained subroutine) overrides a module declaration?
2. If not, is there a way to declare variables in a module to prevent them from being accessible to the contained subroutines?

If this is not possible now, maybe these could be suggestions for future versions of IVF?

Thanks,
Olivier
0 Kudos
1 Reply
Steven_L_Intel1
Employee
407 Views
1) No.
2) No.

The problem we'd have with issuing a diagnostic for this is that the standard explicitly dictates this behavior and for many it would be what they want. So if we made the warning a default, we'd get complaints. If it wasn't a default, then most people would never see the warning even in cases where it was appropriate.

I do sympathize. The mistake can go in the opposite direction, by the way - failing to declare a local variable and having it pick up the module variable instead. I've been bitten by this (see Doctor Fortran in "Think, Thank, Thunk"), and again there's no way the compiler could have warn me about it.

What I would suggest is a bit of discipline in naming module variables - give them some special prefix that indicates their "global" status. You might even want to distinguish between public and private variables (private variables can still be accessed by contained procedures but not by users of the module.)
0 Kudos
Reply