Using LEN() in function result declaration

Hello all

The following code produces a segmentation fault when compiled with pgf90 (11.2) and run. The problem seems to be the use of LEN=LEN(String) in the declaration of the result of the function. Shouldn’t this work according to the Fortran 90/95 standard?

Regards
Pirmin


FUNCTION ToUpper(String)
IMPLICIT NONE
! Dummy arguments
CHARACTER(LEN=*),INTENT(IN) :: String
CHARACTER(LEN=LEN(String)) :: ToUpper
! Local variables
INTEGER :: ii
CHARACTER :: Ch

ToUpper = String
Do ii = 1, LEN_TRIM(String)
Ch = String(ii:ii)
if (‘a’ <= Ch .and. Ch <= ‘z’) then
ToUpper(ii:ii) = ACHAR(IACHAR(Ch) - 32)
end if
End Do

END FUNCTION ToUpper


PROGRAM test

implicit none

character (250) :: toupper
CHARACTER (250) :: iline = ‘’, outline = ‘’

iline = ’ subroutine advance(pj,itime,ldt,up,vp,wp,usigold,’
outline = toupper(iline)
print *, trim(outline)

END PROGRAM test

Your example works fine with PGI 11.9.

Hi Pirmin,

This code requires F90 calling semantics so you need to add an explicit interface for ToUpper or put ToUpper in a module.

% cat len.f90 

FUNCTION ToUpper(String)
IMPLICIT NONE
! Dummy arguments
CHARACTER(LEN=*),INTENT(IN) :: String
CHARACTER(LEN=LEN(String)) :: ToUpper
!CHARACTER(LEN=*) :: ToUpper
! Local variables
INTEGER :: ii
CHARACTER :: Ch
ToUpper = String
Do ii = 1, LEN_TRIM(String)
Ch = String(ii:ii)
if ('a' <= Ch .and. Ch <= 'z') then
ToUpper(ii:ii) = ACHAR(IACHAR(Ch) - 32)
end if
End Do

END FUNCTION ToUpper

PROGRAM test
implicit none

interface 
  FUNCTION ToUpper(String)
     CHARACTER(LEN=*),INTENT(IN) :: String
     CHARACTER(LEN=LEN(String)) :: ToUpper
  end function ToUpper
end interface

!character (250) :: toupper
CHARACTER (250) :: iline = '', outline = ''

iline = ' subroutine advance(pj,itime,ldt,up,vp,wp,usigold,'
outline = toupper(iline)
print *, trim(outline)

END PROGRAM test
% pgf90 len.f90 -V11.2 ; a.out
  SUBROUTINE ADVANCE(PJ,ITIME,LDT,UP,VP,WP,USIGOLD,

Note that the problem is the same with 11.9.

Hope this helps,
Mat