1

I want to define two variable with the same name from two module For example, I have two modules which are as follows:

module boundary
  implicit none
  real(kind=8)::pi
  save
contains
....
end module boundary

.............

module scheme
  implicit none
  real(kind=8)::pi
  save
contains
....
end module scheme

............

The above let fortran set space for this variable, some submodule aftercontains will use pi, and then in the following program, I want to set both variable(same name) into a specified value and they must be same. Can I do that?

subroutine initialize ()

  use scheme,only:pi
  use boundary,only:pi
  pi=acos(-1.d0)

end subroutine initialize
5
  • What is it you want to do with this? Do you mean to be able to "associate" those two variables so that both would be changed by that one reference, say (something like joint setup for their later use in each module)? Or, as an answer suggests, do you mean to be able to refer to each but as different local names? Commented Feb 17, 2016 at 20:35
  • @francescalus the first is what I want. Commented Feb 17, 2016 at 23:23
  • Ah, then the question did indeed confuse me (and makes the duplicate target and the answer you have wrong). If you add a little detail to what you intend to the question, that is, that pi=acos(-1.d0) is indeed meant to update both module variables, then we can address that. [Most likely, it has to be said, with "you can't do that, but here's probably what you mean".] Commented Feb 17, 2016 at 23:27
  • @francescalus Thank you very much, as you said, I update my question. Commented Feb 18, 2016 at 2:51
  • I've voted to reopen, based on that clarification. Note @HighPerformanceMark's comment about defining "pi" twice (and the edit comment about "submodule"): if you have a fundamental constant a single definition in common may be better which makes this "double-definition" redundant. Commented Feb 18, 2016 at 3:09

1 Answer 1

2

If you have two modules A and B containing the same method foo() then first you can create a local alias with:

program SOModNames
use A, fooA => foo
use B, fooB => foo
implicit none

! Variables
real X(10), Y(10)

call fooA(X,10)
call fooB(Y,10)

end program SOModNames

Unfortunately you cannot scope to a module with call A::foo(X,10) for example.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.