tags:

views:

39

answers:

2

I am using Fortran 90. I have defined a Fortran module in fileA.f as:

module getArr
   double precision a(100)
end module getArr

The same fileA.f contains a subroutine that uses this module:

subroutine my_sub
   use getArr
   implicit none

   a(1) = 10.5

end subroutine

In fileB.f, I have a Fortran function. I am trying to access the value of a(1) as:

double precision function my_func(R)
  use getArr
  double precision x
  x = a(1)

  return 
end

But I am getting errors at the compile time. It says it is unable to access the module getArr. Is this something to do with the use of a module within a function as opposed to within a subroutine ?

How should I declare it?

+1  A: 

It looks like you are trying to use getArr% as some kind of module specifier. Are you sure that's right? I'm not an f90 expert, but my compiler doesn't seem to support anything like that. Once you do a use all the stuff in that module is available locally just like you declared it in your subroutine.

Try removing that getArr% and see what happens.

T.E.D.
sorry, it was a mistake. If one defines a "type" block within the module, then % can be used for the variables withing that type block. Even when I remove that (correction done in the code above) , it gives the same error. Is there something to with use of a module within a "function"?
cppb
+3  A: 

T.E.D. is correct about the syntax -- "getArr%" is not part of the name of the array "a". That notation is used for a user-derived type.

Another aspect that is outside the language standard -- compiling the source code: With most compilers, you need to compile your files in order, placing the source-code file that contains a module before any separate file that uses it. The compiler has to "know" about a module before it can use it.

Also, do you have a main program in your example?

If it still doesn't work, please show us the exact error message.

M. S. B.
@M.S.B. you were correct about the order in which files are compiled. That resolved the probelm thanks!
cppb
@M.S.B.: in `subroutine my_sub`, it is not writing the value for a(1). Is thare any obvious mistake i am doing?
cppb
At least in the code you show, there isn't a write statement. Try "write (*, *) a(1)".
M. S. B.