3

I am writing a function to return a string

function doc () Result s
character (Len=65) :: s
...
end function

Is it possible to have a variable length string, where I can allocate the length of the string being returned. I know I can do it using a subroutine, but not for a function.

Function discl (nm) Result (s)

Character (Len=:), Allocatable :: s 
Character (Len=*), Intent (In) :: nm

Integer :: n
Character (Len=65) :: stamp
stamp = "Thu May  7 15:13:48 BST 2015" 

n = Len_trim (stamp)
Allocate (Character (n) :: s)
s = Trim (fstamp) 

End Subroutine discl
5
  • 1
    What did you try so far? Commented May 7, 2015 at 14:01
  • I cannot use Character (Len=*) :: s Commented May 7, 2015 at 14:04
  • Did you try allocatable strings? Commented May 7, 2015 at 14:06
  • I shall try and tell you what happens Commented May 7, 2015 at 14:06
  • 3
    Yes, it is possible to return an allocatable function result, even a character with deferred length. Without seeing your attempts I can't tell what's going wrong for you. Commented May 7, 2015 at 14:06

2 Answers 2

8

It is the same for a subroutine and for a function. Only the header differs, but you can use the result variable as any other variable. My favourite example is the conversion of an integer to a string

  function itoa(i) result(res)
    character(:),allocatable :: res
    integer,intent(in) :: i
    character(range(i)+2) :: tmp
    write(tmp,'(i0)') i
    res = trim(tmp)
  end function

the result variable is allocated on assignment. You could use an allocate statement before the assignment, but that is redundant.

Sign up to request clarification or add additional context in comments.

9 Comments

I disagree with your last statement. In my experience, allocation by assignment is asking for trouble! You should always allocate before an assignment and deactivate that particular feature at compile-time.
I don't bother, it is standard conforming the same way as automatic deallocation from F95 and even Intel is thinking about making it the default.
What sort of problems have you encountered when you don't allocate?
If you accidentally assign an array with mismatching shape you will not get an error message but the array will silently get re-allocated. This makes de-bugging very tricky.
|
7

You can use allocatable strings for this purpose:

module str_mod
  implicit none

contains 

function str2str(str) result(s)
  implicit none
  character(len=*),intent(in)   :: str
  character(len=:),allocatable  :: s

  allocate( character(len=2*len(str)) :: s )
  s = str // str
end function

end module

program test
  use str_mod
  print *,str2str('test')
end program

1 Comment

Even with the discussion about the merits of the allocate statement in another answer, it's a good reminder how to allocate a deferred length character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.