1
votes

I am new to Fortran and trying to understand if the following is possible. My idea to structure the program is to declare the precision and variable types in one module. Then make use of those variables without declaring again the type in other modules or the main program.

module pre
implicit none

    INTEGER, PARAMETER      ::  sp=SELECTED_REAL_KIND(6,37)
    INTEGER, PARAMETER      ::  dp=SELECTED_REAL_KIND(15,307)
    INTEGER, PARAMETER      ::  qp=SELECTED_REAL_KIND(33,4931)

    REAL(dp), PARAMETER     ::  pi = 4.*ATAN(1.)
    REAL(dp)                ::  H                              
    REAL(dp)                ::  M 
    REAL(dp)                ::  KR 

end module pre

Now I want to make use of all the variables in another module that contains one or more functions, such as:

module hon
use pre
implicit none

contains
    function KE(H,M) result(KR)
        KR = 2*PI/H/M
    end function KE
end module hon

Then I use gfortran in this order:

gfortran -c mod_pre.f90
gfortran -c mod_hon.f90

Since 'module pre' is part of 'module hon' I compile in order, but gfortran shows an error.

With the code above I understand the variable types and parameters should have been included by USE; But the message I get from gfortran is that none of my variables have IMPLICIT type when I try to compile 'module hon'.

Could somebody clarify the problem or suggest a solution? I would like to avoid having my variables scattered in multiple modules.

Thanks!

1

1 Answers

0
votes

In the function statement, the result(kr) says that the function result has name kr. This function result is not the same thing as the module variable kr. In particular, this function result makes inaccessible the module variable.

The function result is specific to the function itself and its properties must be declared within the function subprogram.

Similarly, the dummy arguments of the function, H and M, are distinct from the module variables and need to be declared in the function subprogram.

Beyond that, you perhaps have similar concerns to this other question.

To be clear, it isn't possible to say something like "all function results called kr and all dummy arguments called H or M have these characteristics". Each individual object must be given the properties.

However, although I don't recommend this, this is a situation where literal text inclusion (using a preprocessor or include file) could help you:

function ke(H, M) result (kr)
   include 'resdummydecls'
   ...
end function

where the file has the declarations.