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

Purpose of IMPLICIT NONE statement in a module

OP1
New Contributor III
2,938 Views

The recent thread on IMPLICIT NONE reminded me of athought I had a while agoregarding the utility of IMPLICIT NONE in a module.
Let's assume that I wrote a module as:

MODULE MYMOD
IMPLICIT NONE
INTEGER MYVAR
END MODULE MYMOD

What purposedoes the IMPLICIT NONE statement serve here? Is it actually possible to have implicitly declared variables in a module? It seems to me that omitting IMPLICIT NONE would not change anything in my example.
Does anybody have an idea or example showing a case where the IMPLICIT NONE statement is necessary?

Thank you in advance!

Olivier

0 Kudos
3 Replies
Steven_L_Intel1
Employee
2,938 Views
IMPLICIT NONE is never necessary - but it is always a good idea. It prevents you from using Fortran's implicit typing rules, which can lead to unintentional errors.

Let's say that your module had this instead:

MODULE MYMOD
DIMENSION X(3)
END MODULE MYMOD

X is implicitly typed "default REAL". Maybe that's what you want, maybe not. More interesting is when the module contains procedures. In that case it is easier to miss declaring a module.

Perhaps you thought that a type statement was the only way to declare a variable in a module - not so.
0 Kudos
OP1
New Contributor III
2,938 Views
IMPLICIT NONE is never necessary - but it is always a good idea. It prevents you from using Fortran's implicit typing rules, which can lead to unintentional errors.

Let's say that your module had this instead:

MODULE MYMOD
DIMENSION X(3)
END MODULE MYMOD

X is implicitly typed "default REAL". Maybe that's what you want, maybe not. More interesting is when the module contains procedures. In that case it is easier to miss declaring a module.

Perhaps you thought that a type statement was the only way to declare a variable in a module - not so.

Ah, you nailed it.
Obviously the following syntax is not valid:

MODULE MYMOD
X
END MODULE MYMOD

but as you pointed out, this works
MODULE MYMOD
DIMENSION X(3)
END MODULE MYMOD

That's interesting (a bit bizarre, maybe). I always use IMPLICIT NONE - but never declared variables in a module by omitting their type.

Thanks Steve.
0 Kudos
Steven_L_Intel1
Employee
2,938 Views
The use of non-type statements such as COMMON or DIMENSION or NAMELIST are common (!) in older code. I see a lot of people just grabbing all their INCLUDE files and dumping them into a module and they can have issues with implicit typing that way.

Even IMPLICIT NONE won't protect you from this frequent mistake:

[plain]module mymod
implicit none
integer i
contains
subroutine sub(j)
integer j
do i=1,j
...
[/plain]
Note that in the subroutine, variable I is undeclared. But that's ok, because it sees the module variable I instead. Was that meant? Maybe yes, maybe no. Often, when old code is being converted, the answer is no. This example was contrived, but I've seen real applications fall afoul of unwanted host association. Unfortunately, there is no way to require declaration of I in the subroutine so that you get an error if you omit it. If you DO declare it there, it hides the module-level declaration.
0 Kudos
Reply