I have a simple function that returns a time object based on a time string:
FUNCTION getTime(timeStr)RESULT(time)
IMPLICIT NONE
CHARACTER(LEN=19),INTENT(IN) :: timeStr
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: time
READ(UNIT=timeStr( 1: 4),'(I4)')time%yyyy
READ(UNIT=timeStr( 6: 7),'(I2)')time%mo
READ(UNIT=timeStr( 9:10),'(I2)')time%dd
READ(UNIT=timeStr(12:13),'(I2)')time%hh
READ(UNIT=timeStr(15:16),'(I2)')time%mm
READ(UNIT=timeStr(18:19),'(I2)')time%ss
ENDFUNCTION getTime
I call it from the parent routine as:
umwmTime1=getTime(umwmStartTimeStr)
umwmTime2=getTime(umwmStopTimeStr)
where umwmTime 1 and 2 are declared as:
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: umwmTime1,umwmTime2
The compile error message I get is:
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 282)
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 283)
0 inform, 0 warnings, 2 severes, 0 fatal for umwm_component_run
Lines 282 and 283 point are function calls in the parent routine.
However if I use subroutine (instead of function) to get umwmTime1 and umwmTime2 as INTENT(OUT) arguments, I get no problems. What am I doing wrong with the function?
The problem is the compiler doesn’t know that the timetype you defined in the main program is the same as the timetype that you defined in the function. You should define that one place, preferably in a module, and let that define the type everywhere.
For instance, in a simple one-file program, the code you provided doesn’t work for me in gfortran, but this does: