Question

I am new to Fortran and I had made a program where everything did fit into one single file, which worked. However when i tried to take out parts and put them into modules and subroutines I quickly ran into problems. Even after removing everything and having only the bare minimum left, it still gives an error regarding the subroutine.

Currently the heavily reduced main program looks like this. It only uses the module and calls the subroutine.

program test
    use mod1
    call sub1(var)
end program test

and the module looks like:

Module mod1
    implicit none
    type A
        real                     :: type1
    end type A

contains
    subroutine sub1(var)
        type(A)  :: var
        var%type1 = 1+1
    end subroutine sub1

However I seem to do something wrong here and unfortunately I can not figure out what. I get the error

 ||Error: Type mismatch in argument 'var'  ; passed REAL(4) to TYPE(a)|
end module mod1

Can someone please explain which fundamental mistake I am making in order to prevent the most basic subroutine from working?

Was it helpful?

Solution

In your program you do not explicitly declare var. In days of yore Fortran supported implicit typing and, by default, variables whose name begin with a v will be of type real. Fortran retains this capability, though its use is now frowned upon.

I think you are thinking (as if I had a clue what you are thinking) that var in the program scope will somehow be automatically associated with, or the same as, var in the subroutine in the module. It won't be.

Do these things:

  • Insert implicit none on the line after use mod1 in your program.
  • Explicitly declare var within the program, eg type(a) :: var
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top