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 use
s 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?
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 ofuse
statements, for examplea
accessingb
andb
accessinga
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With