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=*)).

$.02 -Ron Shepard

As I understand it, a character(*) function returns a character
variable whose length is fixed by its caller. In the example below the
first program attempts to return a result of indeterminate length and
compilation fails. The second program does return a value which is then
truncated.

C:\g77>type foo1.f
character(*) zz

print *,'|',zz(),'|'
end

character(*) function zz()
zz='hello, world'
end

C:\g77>g77 foo1.f
foo1.f: In program `MAIN__':
foo1.f:1:
character(*) zz
1
foo1.f:3: (continued):
print *,'|',zz(),'|'
2
Invalid declaration of or reference to symbol `zz' at (2) [initially
seen at (1)
]

C:\g77>type foo2.f
character*10 zz

print *,'|',zz(),'|'
end

character(*) function zz()
zz='hello, world'
end

C:\g77>g77 foo2.f

C:\g77>a
|hello, wor|

C:\g77>

[I hope this rectifies my previous lack of understanding of this
subject. :-)]

e-mail: epc8 at juno dot com

.



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)