title: what is the correct way to write a specific interface in fortran?
body: <p>I tried the following code to get the code to work correctly. It seems I need to write an interface to avoid mistaken results. I wrote the following, but it does not pass the compiling stage. The errors are the following: (use gfortran compiler)</p>
  
  <pre><code>fit.f90:34.16:
    TYPE(spec), INTENT(IN) :: SMP
              1
  Error: Derived type 'spec' at (1) is being used before it is defined
  fit.f90:35.12:
    REAL(q), INTENT(OUT) :: RES
          1
  Error: Symbol 'q' at 1 has no IMPLICIT type
  ...
  </code></pre>
  
  <p>Here is the compilable code to illustrate the issue.</p>
  
  <pre><code>MODULE prec
    INTEGER, PARAMETER :: q=8
  END MODULE prec
  
  MODULE MOD_FIT
    USE prec
  
    INTEGER, PARAMETER :: NO_NI=200
    TYPE spec
      ! source of fitting
      INTEGER NW
      COMPLEX(q), POINTER :: W(:),G(:)
  
      ! how spectrum is parametrized
      INTEGER NO_NM        ! continuous points labeled as 1,2,3,...,NO_NM
      REAL(q), ALLOCATABLE :: O_N(:),O_D(:)        ! frequency
      REAL(q), POINTER     :: A_N(:),B_N(:),A_D(:) ! A(Omega)
  
    END TYPE
    TYPE(spec) SMP
  
    INTERFACE
      SUBROUTINE NNLS_CMPLX(SMP,RES,RES_V)
        IMPLICIT NONE
        TYPE(spec), INTENT(IN) :: SMP
        REAL(q), INTENT(OUT) :: RES
        COMPLEX(q), OPTIONAL, INTENT(OUT) :: RES_V(SMP%HN)
      END SUBROUTINE NNLS_CMPLX      
    END INTERFACE
  
  CONTAINS
  
    SUBROUTINE NNLS_CMPLX(SMP,RES,RES_V)
      IMPLICIT NONE
      TYPE(spec) :: SMP
      REAL(q) :: RES
      COMPLEX(q), OPTIONAL :: RES_V(*)
  
      RES=0
      IF(PRESENT(RES_V)) RES_V(1)=0
  
    END SUBROUTINE NNLS_CMPLX      
  
  END MODULE MOD_FIT
  
  PROGRAM MAIN
    USE prec; USE MOD_FIT
    REAL(q) PK_RES
    COMPLEX(q) RES_V(4)
  
    CALL NNLS_CMPLX(SMP,PK_RES,RES_V)
  
  END
  </code></pre>