Re: Allocatables in derived types and functions



Arno <arnoinperu@xxxxxxxxxxx> wrote:

[has a function get_i, which returns an allocatable result]

Now I was wondering if I am allowed to use this function as an argument
in a function call?

Yes, (all assuming that the compiler supports the allocatable TR in
order to have allocatable components and allocatable function results at
all), however...

For example:

call write_i(get_i(object))

That is *NOT* an example of using the function as an argument in a
function call. If you had

call write_i(get_i)

then that would be using the function as an argument in a function call.
Both forms are allowed (with appropriate changes in write_i), but they
mean completely different things.

What you have is an example of using the function *RESULT* as an
argument. The difference is quite important. That does also happen to be
allowed.

subroutine write_i(i)

integer,dimension(:),intent(in) :: i

Here, I think, is your problem. It actually has nothing to do with your
function get_i at all.

Your subroutine write_i has an assumed-shape dummy argument. Tis means
that write_i requires an explicit interface. Since you didn't mention
this or showanything that obviously provides one, I'm betting that you
don't have one and that this is your problem.

Pretty much all of the features that are new to f90 or later and have
anything to do with procedure arguments require an explicit interface.
This is one of them. Functions with allocatable results are another. I
recommend routine providing an explicit interface for all procedures,
whether they require it or not. This keeps you from having to memorize
the list of conditions and remembering to think about it all the time -
particularly if you change a procedure in a way that adds the
requirement.

If you are using the kinds of things shown in your example, you are
likely to run into explicit interface requirements a lot.

The simplest way to provide an explicit interface is to put all the
procedures in modules; that makes the explicit interfaces automatic.

If your procedures are already all in modules, then perhaps it is
something else. But you didn't explicitly mention that, and it is a
*CRITICAL* point. This makes me think that you are not aware of the
issue, or perhaps forgot about it when posting this query. (Lots of
people aren't aware of the issue; variants of it are high on the
frequently asked questions list here).

It is also true that, even with the required explicit interfaces, your
example is a reasonable "stress test" for implementation of the
allocatable TR. (And actually passing the function would be another good
test). But I think we need to make sure that the code is actually
correct before going there.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
.



Relevant Pages

  • Re: Passing pointers to derived types to subroutines
    ... Intel Fortran Compiler v9.1): ... You have failed to provide an explicit interface for the subroutine ... Having a pointer dummy argument is one of the many ...
    (comp.lang.fortran)
  • Re: optional routine arguments
    ... Your original code is not legal Fortran, because it lacks the explicit interface for a routine with optional arguments. ... The problem is that the compiler has no way to know that it needs to pass something special to indicate that you omitted k. ... Current Intel Fortran with interface checking on gives, ... Developer Products Division ...
    (comp.lang.fortran)
  • Re: help for program
    ... I suppose your compiler must accept them as an extension, ... Error: clf.f90: Explicit interface required for CREAALBEROF from\ ... I call this unlucky because it can mislead you into ... as I didn't try to fix it and then check further. ...
    (comp.lang.fortran)
  • Re: Any suggestions?
    ... form of argument reference **MUST** have what is called an explicit interface. ... Interface blocks and modules are both Fortran 90 features. ... The C command line functionality has been around since C first got started, i.e., for over 20 years. ... Not all Fortran compilers support it yet as part of the Fortran compiler. ...
    (comp.lang.fortran)
  • Re: glibc detected *** ./run.out: munmap_chunk(): invalid pointer:
    ... the code is working fine with ifort but fails on other compiler. ... if I am trying to use dynamic allocation, ... I'm almost certain that your problem is that the subroutine doesn't have ... an explicit interface in the place where it is called from. ...
    (comp.lang.fortran)