Silverfrost Forums

Welcome to our forums

Character Function Only Returns String of LEN=1

21 Aug 2017 9:52 #20038

Hello, everyone,

The following function only returns the first letter of the input string and I can't put my finger on why that is, so I'm asking for your advice once more.

And thanks a lot for all the help provided so far. It's made getting started with Fortran a lot less frustrating than I had it expected to be.

CHARACTER FUNCTION get_filepath(messg)
    CHARACTER FUNCTION get_filepath(messg)
    CHARACTER(LEN=260)::get_filepath,reval
    CHARACTER(LEN=*),INTENT(IN)::messg

!   Get filepath
    PRINT*,messg
    READ*,reval
    get_filepath=reval           
END FUNCTION get_filepath
21 Aug 2017 12:05 (Edited: 21 Aug 2017 12:08) #20039

Do you have declared your variables outside of the function correctly? Seems to work for me.

      PROGRAM TEST
C
      character*260 msg1,msg2,get_filepath
      msg1='test1'
      msg2='test2'
      msg1=get_filepath(msg2)
      write(*,*)'---'
      write(*,*)msg1
      END
21 Aug 2017 12:06 #20040

A function or variable, declared to be of type CHARACTER, with no length specified, has a length of 1.

If that is not what you want, you must clearly stipulate what you wish to do, and declare the function to be of type CHARACTER(len=...), where the length can be a predetermined constant, or calculated in some way.

21 Aug 2017 4:42 #20043

Thanks!

I declared the variables correctly, but I didn't declare LEN=260 for get_filepath outside of the function.

Please login to reply.