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

variable scope in module specification

tracyx
New Contributor I
567 Views
Dear All,

Suppose I have a module like the following.

module test
use global, only : ndim
real(8), dimension(ndim) :: y
contains
...
end module

Is it possible to limit the scope of variables which appear in the specification part of the module? In particular, is it possible to restrict the scope of ndim so it is only used to define the dimension of y, and is not visible in the subroutines that the module contains?

Thanks, T.
0 Kudos
3 Replies
mecej4
Honored Contributor III
567 Views
Use the PRIVATE and PUBLIC attributes as needed. For example, with the following source codes:

1. glob.f90:

[fortran]module global
  integer,parameter :: ndim=10,mdim=5
end module global[/fortran]
2. ugl.f90:

[fortran]module test
  use global, only : ndim
  implicit none
  private :: ndim
  real, dimension(ndim),public :: y
end module test[/fortran]
3. prg.f90:
[fortran]program testprg
use test
  implicit none
  write(*,10)lbound(y),ubound(y),size(y)
  write(*,10)ndim
10 format(1x,3i6)
end program testprg[/fortran]
an attempt to compile prg.f90 would give you the compiler error message:

[bash]prg.f90(5) : Error: This name does not have a type, and must have an explicit type.   [NDIM]
  write(*,10)ndim
-------------^
compilation aborted for prg.f90 (code 1)[/bash]
If you then remove the second WRITE statement, the program should compile and run fine.
0 Kudos
abhimodak
New Contributor I
567 Views
The original question seems to be asking if there is a way to restrict host-association i.e. the "contained" procedure does not see entities defined in the host. I don't know if it is possible... {The well-known exception to host-association being the interface blocks i.e. use of IMPORT statement.}

Abhi
0 Kudos
mecej4
Honored Contributor III
567 Views
That was the second part of the original question, and I see that did not address this part.

Metcalf, Reid & Cohen's Fortran 95/2003 Explained states in Sec. 5.6: "An internal subprogram automatically has access to all the host's entities...".

Although access to host variables cannot be hidden, one could use local variables with the same names as the host variables, but this,too, has obvious drawbacks.
0 Kudos
Reply