Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a module more than once

Tags:

module

fortran

Suppose I have a module which defines some basic constants such as

integer, parameter :: i8 = selected_int_kind(8)

If I include this in my main program and I also include a module which does some other things (call this module functions) but functions also uses constants, then am I essentially including constants twice in my main program?

If so, is this bad? Can it be dangerous at all to include a module too many times in a program?

like image 935
drjrm3 Avatar asked Feb 03 '12 13:02

drjrm3


1 Answers

No, it is fine to do this. All you are doing with the use statement is providing access to the variables and functions defined in your module via use association. It is not like declaring variables each time they are use'd (they are in fact redeclared however).

The only thing to be wary of are circular dependencies, where module A uses module B and module B uses module A. This is not allowed.

Edit: From Metcalf et al. Fortran 95/2003 explained, pg. 72:

A module may contain use statements that access other modules. It must not access itself directly or indirectly through a chain of use statements, for example a accessing b and b accessing a.

Whilst this quote doesn't directly answer your question, it reiterates that really the only thing you can't do is have a circular dependency. So the following is perfectly valid:

module one_def
  implicit none
  integer, parameter :: one=1
end module one_def

module two_def
  use one_def, only : one
  implicit none
  integer, parameter :: two=one+one
end module two_def

program test
  use one_def, only : one
  use two_def, only : two
  implicit none

  print*, two == one+one ! This prints .True.

end program
like image 152
Chris Avatar answered Oct 07 '22 07:10

Chris