Re: Proper way to return a string



In article <1167448598.037577.155500@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"Beliavsky" <beliavsky@xxxxxxx> wrote:

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

The following code is legal f90, and without the result(), it would
be legal f77:

function str_func(aa) result(bb)
character (len=*) :: bb
character (len=*), intent(in) :: aa
bb = aa
end function str_func

I don't know why your original code does not compile. I expect that
it is some subtle difference between module functions (which are f90
only) and external functions (which are common to both f77 and f90).

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.

The compiler "knows" the length of the function result the same way
it "knows" the length of the argument. Namely, it is determined by
the calling routine.

$.02 -Ron Shepard
.



Relevant Pages

  • Re: Help.. subroutines etc..
    ... and i cant find where i ve done mistake! ... You probably got some kind of compiler error message. ... Your subroutine is sitting in the middle of the main program. ... If you are using a compiler capable of at least f90, ...
    (comp.lang.fortran)
  • Re: Question on allocatable variable in a subroutine
    ... You mention f90. ... Allocatable arguments require an f95 compiler that has the ... Pointers can be used to do this kind ... subroutine must be explicit where it is called. ...
    (comp.lang.fortran)
  • Re: Question on allocatable variable in a subroutine
    ... allocated within the subroutine. ... You mention f90. ... Allocatable arguments require an f95 compiler that has the ... No, pointer will not work. ...
    (comp.lang.fortran)
  • Does F90 do type checking when calling module subroutines?
    ... I was under the impression that if you call subroutines from modules, f90 ... I have a subroutine defined in a module like this: ... END INTERFACE ... any way I can convince the compiler to let me know of my evil ways? ...
    (comp.lang.fortran)
  • Re: DPROD issues
    ... a switch like that typically ... makes a compiler nonstandard in that mode. ... treatment of specific intrinsics is one ... subroutine sub1a ...
    (comp.lang.fortran)