I\'m pretty new in the Fortran world. I get a piece of code, where I find difficulty in understanding it.
Let\'s say in module A, var
is declared as a
There will be a compile time error when you attempt to access the variable var
in your described case. The specific error will look like:
Error: Name 'var' at (1) is an ambiguous reference to 'var' from module 'a'
Are those variables meant to be globally scoped? You can declare one (or both) of them with private
so they are scoped to the module and do not pollute the global scope. In this case though, module C would not be able to use the private variable. Another option is to limit what is imported in your use
statement with:
use A, only: some_variable1, some_other_variable
use B, only: var
This would let var
from B into the scope of C, and would hide var
from A.
If you have to have both variables in module C, you can rename one when you use the module. E.g.:
use A, varA => var
use B, varB => var
which lets you access the variables var in each module by the names varA
and varB
.
See this example:
module A
integer, parameter :: var = 81
contains
end module
module B
integer :: var(2)
contains
end module
module C
use A, varA => var
use B, varB => var
contains
end module
program test
use C
print *, varA
end program
which will print 81
.