Externally created parametrized derived type

Viewed 76

I would like to create a parametrized derived type inside a procedure and operate on the type's member. The following code works and writes out 4 and 8, as expected.

module mod1
  implicit none
  type :: pdt(n)
    integer, len :: n
    integer :: a(n), b(n)
  end type
end module mod1

program main
  use mod1
  implicit none
  type(pdt(4)) :: aa

  print *, aa%n
  print *, func(4)

  contains
    integer function func(n) result(m)
      integer :: n
      type(pdt(n)) :: pdttest

      m = 2*pdttest%n
    end function
end program

The problem arises when I place the procedure func in a separate module, in which case pdttest%n is uninitialized. Compiling using gfortran 11.2.0 with the flag -Wall returns: Warning: ‘pdttest.n’ is used uninitialized [-Wuninitialized] What could be the cause of this?

 module mod2
  use mod1
  implicit none
  contains
    integer function func(n) result(m)
      integer :: n
      type(pdt(n)) :: pdttest

      m = 2*pdttest%n
    end function
end module
0 Answers
Related