Re: Proper way to return a string



Beliavsky wrote:
Ron Shepard wrote:
In article <2006122715420316807-gsande@worldnetattnet>,
Gordon Sande <g.sande@xxxxxxxxxxxxxxxx> wrote:

Probably because for a character function you need to declare a
fixed size but it can vary if you use a subroutine.

Assuming that you use Fortran character variables for "strings". ;-)
Perhaps I misunderstand, but I think you have the same flexibility
with functions and subroutines. Since f77, both allow the result to
be declared as assumed length (i.e. (*), or in f90, (len=*)).

I don't think a function have a character(len=*) RESULT . For example,
for the following code

module string_func_mod
implicit none
contains
subroutine str_sub(aa,bb)
character (len=*), intent(in) :: aa
character (len=*), intent(out) :: bb
bb = aa
end subroutine str_sub
!
function str_func(aa) result(bb)
character (len=*), intent(in) :: aa
character (len=*) :: bb
bb = aa
end function str_func
end module string_func_mod

g95 says

In file string_func.f90:10

function str_func(aa) result(bb)
1
Error: Internal function 'str_func' at (1) cannot be an assumed-length
CHARACTER

For a subroutine argument, the LEN of the character argument is the
same on output as on input, but for a function, the compiler would not
"know" what the LEN of a character (len=*) RESULT is.


This is why TRIM() can never be implemented in F77, despite the claims to the
contrary by certain c.l.f regulars. Ultimately, there is no way in F77 to return
a string from a routine whose length is not known when the routine is entered.

cheers,

Rich
.



Relevant Pages

  • Why use modules?
    ... in the 2nd example than with F77. ... becomes necessary to utilize these advanced features there is ... subroutine mysub! ... $ fort myprog.f77 ...
    (comp.lang.fortran)
  • Re: TRIM
    ... character string1*10,string2*8 ... call dummy) stop end subroutine dummycharacter stringarray*1,string* ... Please see 5.1.2 of the f77 standard. ...
    (comp.lang.fortran)
  • Re: array slice
    ... Array slices were "new" to f90 (which is ... happy to not be using f77, but I'm not paying the bills... ... As for the F90 slices, when passing a row Ato a subroutine, do ...
    (comp.lang.fortran)
  • Re: Structure of large link libraries in f95
    ... simply INCLUDEing f77 files, so as not to muck up the originals? ... following try hangs up on the gfortran requirement for "END SUBROUTINE" ... make a module with nothing but interface blocks. ...
    (comp.lang.fortran)
  • Re: Transmission of character string by argument
    ... Actual character argument at is shorter in length ... For a routine which prints messages, ...
    (comp.lang.fortran)