Van described the method that reflects modern programming preferences,
and is what I would have done as well. However, your original example
could have also been written as
subroutine some_sub (a, f)
real :: a
call f(a)
end subroutine
Declaration of the procedure is not required in this case. The
compiler can deduce from context that f is a subroutine. This form will
work fine with f90. What you give up with this form is argument type
checking in the subroutine call.
Cheers,
Bill
Tobias Brandt wrote:
> Thank you very much for your quick answer. At least now I know I don't
> have to search for a Fortran 90 solution any longer. :-)
>
> On 6 August 2010 00:40, Van Snyder <[log in to unmask]
> <mailto:[log in to unmask]>> wrote:
>
> Tobias:
>
> It's not possible in Fortran 90 or Fortran 95, but in Fortran 2003 or
> Fortran 2008 you can define an abstract interface thus:
>
> abstract interface
> subroutine F ( Arg )
> real :: Arg
> end subroutine F
> end interface
>
> You can then access that interface either within the same scope as its
> definition, or by use or host association, and declare a procedure
> pointer, procedure dummy argument, or type-bound procedure by reference
> to it:
>
> subroutine Some_Sub ( A, My_F )
> real :: A
> procedure(F) :: My_F
> ....
> end subroutine Some_Sub
>
> See subclauses 4.5.3, 4.5.4, 12.3.2.1 and 12.3.2.3 of
> http://j3-fortran.org/doc/year/04/04-007.pdf.
>
>
> On Thu, 2010-08-05 at 15:09 -0700, Tobias Brandt wrote:
> > Hi all,
> > I'm just starting out with Fortran 90 coming from C++ and need some
> > help. In C++ you can define a function pointer type like so:
> >
> >
> > typedef void (*mytype) (float);
> >
> >
> > and then use it as a type for function parameters:
> >
> >
> > void some_function (float a, mytype f)
> > {
> > f(a);
> > }
> >
> >
> > void some_other_function (float a, mytype f)
> > {
> > f(2*a);
> > }
> >
> >
> >
> >
> > Now, if I want to do the same in Fortran 90, I have to define the
> same
> > inteface twice:
> >
> >
> > subroutine some_sub (a, f)
> > real :: a
> > interface
> > subroutine f (arg)
> > real :: arg
> > end subroutine
> > end interface
> > call f(a)
> > end subroutine
> >
> >
> > subroutine some_sub (a, f)
> > real :: a
> > interface
> > subroutine f (arg)
> > real :: arg
> > end subroutine
> > end interface
> > call f(2*a)
> > end subroutine
> >
> >
> >
> >
> > Is it possible to somehow "reuse" the interface definition by
> > declaring it outside the subroutines or in a different module?
> >
> >
> > Regards,
> > Tobias
>
>
--
Bill Long [log in to unmask]
Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101
|