Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
25 Views

Scope of IMPLICIT NONE through a USE statement

If a parent module has an IMPLICIT NONE statement and this module is accessed through a USE statement in a descendant module without an IMPLICIT NONE statement, does the IMPLICIT NONE apply to descendant module? In my recent experience, the implicit none does not. As a result, some of my legacy code generates compilation errors or worse still, continues executing with unrealistic numbers. This seems to be irrespective of compiler settings. An example is included below. Any help would be greatly appreciated.

module parent

implicit none

integer(kind = 4) :: a, b

contains

subroutine psub()

a = 1

b = 2

! c = 3 ! generates compilation error since c is not declared

print *, 'a = ', a

print *, 'b = ', b

! print *, 'c = ', c ! generates compilation error since c is not declared

end subroutine psub

end module parent

module descendant

use parent

contains

subroutine dsub()

d = 4 ! does not generate compilation error even though d is not declared

call psub()

call tsub(i)

print *, 'd = ', d ! prints d as a single precision number 4.000000

read *

end subroutine dsub

subroutine tsub(i)

integer(kind = 4) :: i

print *, 'In tsub: i = ', i ! prints i = -858993460 since expected argument is mismatched in type

end subroutine tsub

end module descendant

program main

use descendant

call dsub()

end program main

 

0 Kudos
4 Replies
Highlighted
Employee
25 Views

It does not carry into the scope of the contained routine or other module. This was discussed in part (your variable c) in an earlier thread, https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/287958.

Specifically regarding inheritance through USE, Advice from others is, it does not. USE makes declared entities accessible, but it doesn’t carry along other aspects of the module such as IMPLICIT.

 

0 Kudos
Highlighted
New Contributor I
25 Views

Thanks, @Kevin D. So to be clear, with reference to my example above

1. the implicit none declared in module parent will apply to any subroutines or functions contained in the parent module.

2. the implicit none declared in module parent will not apply to any module such as module descendant that invokes parent through a use statement.

0 Kudos
Highlighted
Employee
25 Views

Yes, that is correct. My apologies, I "mis-spoke" in my reply as to your item 1.

 

0 Kudos
Highlighted
New Contributor I
25 Views

Thanks, @Kevin D - that's what I thought. Now clear and confirmed by running the code.

0 Kudos