- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page