1

I have a FORTRAN 95 code where, in the main program, these variables are declared:

integer :: i
real(8) :: dx                               
real(8), allocatable :: X(:), Y(:), Z(:)

The following function uses these values at some point during the program:

function Funcion_ceros(var_4)           
        implicit none
        real(8), intent(in) :: var_4(2)
            real(8) :: Funcion_ceros(2)
            Funcion_ceros(1) = var_4(1) - Y(i) - dx*var_4(2)
            Funcion_ceros(2) = var_4(2) - Z(i) - dx*F(X(i + 1), var_4(1), var_4(2))
end function Funcion_ceros

If I include this function in the contains section of the main program, there is no compiling issue. However, when separating it into a module, it loses access to these variables. I attempt to specify the same variabes written above in the module aswell, and get the following error:

Symbol 'i' at (1) conflicts with symbol from  module 'Module_Name', use associated at (2).

How do I separate this function into a module, allowing at the same time its access to the variables it uses from the main program?

Steve
  • 41
  • 6

1 Answers1

2

You can put the variables into the module as well, then the use statement makes them available to the main program as well.

module scope_mod
    implicit none
    character(len=4) :: a = "mod "
    character(len=4) :: b = "mod "
    character(len=4) :: c = "mod "
contains
    subroutine sub()
        implicit none
        character(len=4) :: b
        a = "sub "
        b = "sub "
        write(*, *) "in sub: A from ", a, ", B from ", b, ", C from ", c
    end subroutine sub
end module scope_mod

program scope_test
    use scope_mod
    implicit none
    a = "main"
    write(*, *) "in main: A from ", a, ", B from ", b, ", C from ", c
    call sub()
    write(*, *) "after sub: A from ", a, ", B from ", b, ", C from ", c
end program scope_test

Output:

 in main: A from main, B from mod , C from mod 
 in sub: A from sub , B from sub , C from mod 
 after sub: A from sub , B from mod , C from mod 

Update: I noticed that you said you put the variables in the module 'as well' -- If you have them in the module, they will be made available to the main program by the use statement. If you then declare another variable with the same name, it will conflict. Declare the variables only in the module, and they will be available to both.

chw21
  • 7,970
  • 1
  • 16
  • 31