From: Thomas Robitaille on
Hello,

The following code does not compile:

program test

implicit none

integer,parameter :: sp = selected_real_kind(p=6,r=37)

interface
real(sp) function add(a,b)
implicit none
real(sp),intent(in) :: a,b
end function add
end interface

end program test

My understanding is that this is because the interface block has does
not see any variables outside itself. How can I get around this in the
above case?

Thanks for any help,

Thomas
From: Thomas Robitaille on
On Jul 29, 4:56 pm, Thomas Robitaille <thomas.robitai...(a)gmail.com>
wrote:
> Hello,
>
> The following code does not compile:
>
> program test
>
>   implicit none
>
>   integer,parameter :: sp = selected_real_kind(p=6,r=37)
>
>   interface
>      real(sp) function add(a,b)
>        implicit none
>        real(sp),intent(in) :: a,b
>      end function add
>   end interface
>
> end program test
>
> My understanding is that this is because the interface block has does
> not see any variables outside itself. How can I get around this in the
> above case?
>
> Thanks for any help,
>
> Thomas

I just realized that this is provided by the 'import' statement. I
found the info in an old thread:

http://coding.derkeiler.com/Archive/Fortran/comp.lang.fortran/2003-11/0717.html

Cheers,

Thomas

From: Richard Maine on
Thomas Robitaille <thomas.robitaille(a)gmail.com> wrote:

> The following code does not compile:
>
> program test
>
> implicit none
>
> integer,parameter :: sp = selected_real_kind(p=6,r=37)
>
> interface
> real(sp) function add(a,b)
> implicit none
> real(sp),intent(in) :: a,b
> end function add
> end interface
>
> end program test
>
> My understanding is that this is because the interface block has does
> not see any variables outside itself. How can I get around this in the
> above case?

In f90/f95, pretty much the only way to do it is to put anything that
you want to import into an interface body in a separate module. Then USE
that module in the interface body. (Just a *VERY* picky terminology nit.
It is the interface body that has the problem - not the interface block.
I well understand the confusion as I used to regularly get those terms
confused myself. The interface block is the whole thing from INTERFACE
to END INTERFACE. It doesn't have anything to do with name scoping. The
interface body is the declaration for the particular function. There can
be multiple interface bodies in an interface block.)

In f2003, you could use an IMPORT statement.

As an aside, I personally prefer a function declaration style that
declares the function type with a separate type declaration statement
instead of on the function statement. The reason I mention it here is
that it becomes relevant as soon as your sp comes from a USE statement
or IMPORT statement within the interface body. If you do something like

real(sp) function add(a,b)
use some_module_that_defines_sp
...

Then you have the complication that sp is referenced before the USE
statement that makes it acessible. Havibg an IMPORT statement instead of
a USE raises simillar questions. There are some funny special caes that
allow some things like this, but I don't even recall the fine points of
those special cases. For example, it might be that the special-case
rules for a derived type could be different from those for a type
parameter. I don't actually recall fro sure whether the above is allowed
or not. I'll let someone else check that. Checking might even require
looking into interp requests. If you write instead

function add(a,b)
use some_module_that_defines_sp
real(sp) :: add
...

then you don't have to worry about the details of any special cases
because you aren't depending on them. The sp is referenced after the USE
statement, without anything special comming up. I know that form is fine
without needing to check anything.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain