Re: Must declared length of a character function be matched by any procedure declaring it? YES



"John" <urbanjost@xxxxxxxxxxx> wrote in message
news:2a7f5e48-4679-4851-a188-b9d29d6d4a4b@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm wondering if I have developed an unneccessary habit. I have
assumed
that if I had a character function that was declared with an explicit
length that any routine that used the function had to declare it to be
of the same length (this goes back to pre-module/pre-interface days).

Somewhat in support of that someone was having problems with a large
mostly f77-ish code where strings were being returned with bad values
by a function where there was a mismatch in lengths; when changes were
made so the
length was consistent the problem went away.

But then I made a short test program while discussing this issue with
the code's developer, assuming the compiler would quickly support that
this was the case (by failing). But to my chagrin the following
little
test got no warning messages from any compiler I tried ...

That's because the code is in F77 mode, and errors like that usually
cannot be diagnosed.

program testit
character(len=8),external::bigfunc ! declare function as 8
characters
write(*,*)'string=['//bigfunc()//']'
end program testit
character(len=16) function bigfunc() ! function is defined as 16
characters
bigfunc='ABCDEFGHIJKLMNOP'
end function bigfunc

Try the following:

program test
character(len=8) :: t
t = bigfunc()
print *, t
print *, '[' // bigfunc() // ']'

contains

function bigfunc() result (s)
character(len=16) :: s
s = 'ABCDEFGHIJKLMNOP'
return
end function bigfunc
end program test

So if the function declares itself to be of one length, but references
to it use another, is the behavior just equivalent to
stringa=stringb
as far as truncation or padding to the right with blanks (depending of
course
on which string variable is bigger); or is this
prohibited?

Definitely prohibited. It always has been.
The reason that the error is not detected is that there is no explicit
interface.
Without an explicit interface, the compiler is relying on YOU to get it
correct.
If you don't get it correct, anything can happen because the program is
wrong.
Typical symptoms are wrong results or a a program crash.
Or, as you noticed, wrong results.

In Fortran 90 and later, a character function can return a string
of any length. The length can be constant, or it can depend on
the value or length of some argument. Don't forget to use an
explicit interface though.

The answer didn't leap out at me from the standard and I'm
hoping someone knows off the cuff with authority?

The good news for me would be that this code hasn't been doing
something
wrong for a considerable time; the bad news is that I will then be in
the position of not knowing why the changes that were made fixed the
problem -- and some of the answers are not good (probably smacking
memory
somewhere else, for example)

John Urban


.



Relevant Pages

  • Re: option explicit
    ... Explicit", it might be moments of debugging time instead of hours. ... And one more reason I like it is that when I declare an object explicitly: ... dim myWksThatShowsTheSummary as Worksheet ... I can type mywks and hit ctrl space bar to have the VBE autocomplete (or show me ...
    (microsoft.public.excel.misc)
  • Re: Garbage collection
    ... As you know very well, a GC has advantages and drawbacks, both technical and human ("no free beer" (TM)), so, imho, there are sometimes situations in which a GC is recommended, but sometimes not. ... An on/off global switch per project is in fact rather a one-way road (tell me, what would you do if you have a project with GC enabled and you decide that for eg. speed reasons, avoiding sloppy code aso. ... That's why I tried to propose an approach to allow the developer to explicitly declare what's GCed or not by using both inclusion and exclusion filtering declarative approach. ... Imho, the GC situation now is something similar with 'var' declaration explicit in Pascal, implicit in VB. ...
    (borland.public.delphi.non-technical)
  • Re: Are these good features to use?
    ... as it's good to explicitly declare what I'm creating. ... You can't use "var" without losing human-readable type information in all scenarios and it's not even legal in other scenarios, so there will always be some variables that should or must be declared with an explicit type. ... With the auto-property, it's an easily recognized pattern and reading it you immediately know everything there is to know about the property. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: lang effort: type conversions
    ... inferencer for the lang I am implementing, I have started to consider some ... Yes I have came to the same idea of using a variant type with either ... explicit declaration or invisible syntatic sugar like you are in functions. ... will declare a mutable one. ...
    (comp.lang.misc)
  • Re: Must declared length of a character function be matched by any procedure declaring it?
    ... that if I had a character function that was declared with an explicit ... length that any routine that used the function had to declare it to be ... end program testit ... The above performs an equivalent action to what you suggest. ...
    (comp.lang.fortran)