Re: Proper way to return a string




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.

.



Relevant Pages

  • Re: Dynamically updating perl variables
    ... character of multi-character parameter values. ... The intention was that, once the data was in a Perl data structure, the calling ... the case of your given data subroutine would return a list that, ... nice to see what a full alternative solution would look like using your ideas. ...
    (perl.beginners)
  • Re: reading more data than the record size (RECL)
    ... it writes an array as one vector. ... character id*56 ... c This subroutine reads a standard file into a one-dimensional ... call row_write(lun, work, ncol) ...
    (comp.lang.fortran)
  • Re: Dynamic character length?
    ... > I need to set character length in the middle of a subroutine. ... allows automatic allocation of allocatables on assignment, ... implementations of it. ...
    (comp.lang.fortran)
  • Re: How to return a variable length substring from a function ?
    ... implicit none ... character(*) function fun ... end function fun ... subroutine g ...
    (comp.lang.fortran)
  • Re: c++ calling fortran and fortran calling C++
    ... >This subroutine must call a function of my program. ... >pointer of my function but I have segmentation fault. ... character argument giving its length. ... >I have a segmentation fault and I don't know where is the problem. ...
    (comp.lang.fortran)