I've established that this is not possible in any flavour of FORTRAN before 2003, and often not then. On the other hand, there are various more or less acceptable workarounds e.g. a fixed-length string longer than necessary, combined with TRIM. This is only OK if one can identify such a length that is not impractically large.
I did find, here:
http://www.star.le.ac.uk/~cgp/f90course/f90.html#tth_sEc7.1
the following tantalizing claim:
*Character functions may return a string with a length which depends on the function arguments, e.g. *
FUNCTION concat(s1, s2)
IMPLICIT NONE
CHARACTER(LEN=LEN_TRIM(s1)+LEN_TRIM(s2)) :: concat ! function name
CHARACTER(LEN=*), INTENT(IN) :: s1, s2
concat = TRIM(s1) // TRIM(s2)
END FUNCTION concat
I have two questions about this:
It does seem to rely on the trick of 'deferring' declaration of the type of the function until computation has begun. I know this is legitimate, but more commonly in my experience it's a matter of taste whether one writes (in pseudo-code) FUNCTION F followed by TYPE F, or simply TYPE FUNCTION F. Is there a legitimate way to render this function with an up-front type declaration? It seems not, and that troubles me :?
I don't see how one would usefully use this function in practice. The function result still has to assigned to a variable of type CHARACTER, say s3, which still has to be of fixed length. One could equally well go more briefly:
s3 (1: len_trim (s1)) = trim (s1) s3 (len_trim (s1) + 1: len_trim (s1) + len_trim (s2)) = trim (s2)
(admittedly, it would be shorter to build the function if there was a lot of this stuff going on, and it would also avoid the possibility of programmer errors in the indexing). So is this function anything more than a proof of concept, as it were?