From: david on

Hello,

It seems that it is possible in fortran to give a function name
to a subroutine/function, to call it later.

Is it possible to give a subroutine adresse to a
subroutine/function, to call it later ?


like in C:
void sub (void (*subroutine)(int param))
{
subroutine(1);
}



Thanks for any tips

david
From: mecej4 on
On 6/9/2010 9:54 AM, david wrote:
> Hello,
>
> It seems that it is possible in fortran to give a function name
> to a subroutine/function, to call it later.
>
> Is it possible to give a subroutine adresse to a
> subroutine/function, to call it later ?
>
>
> like in C:
> void sub (void (*subroutine)(int param))
> {
> subroutine(1);
> }
>
>
>
> Thanks for any tips
>
> david

This has been possible for decades. See, for example, Section 5.12 of
Metcalf, Reid and Cohen, Fortran 95/2003 Explained, OUP, 2004.

If using Fortran 77, read about the EXTERNAL declaration. If using
Fortran 9X, read about procedure arguments and interface declarations.

--mecej4
From: Richard Maine on
david <deyv(a)free.fr> wrote:

> It seems that it is possible in fortran to give a function name
> to a subroutine/function, to call it later.

I'm confused as to what you are saying here. No, it is not possible to
give a function name to a subroutine. Functions and subroutines are
separate. Some compilers might not catch the error, but it *IS* an error
to invoke a function as a subroutine or vice versa.

> Is it possible to give a subroutine adresse to a
> subroutine/function, to call it later ?

Again, subroutines and functions are separate. Both are procedures, and
can do many of the same kinds of things, but you cannot change a
subroutine into a function.

It sounds somewhat like you are asking for procedure pointers, which are
new to the standard in f2003. Those do allow you to pointer assign a
procedure in one place and then reference it in another. But they have
nothing to do with confusing subroutines and functions.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: david on
mecej4 <mecej4_no_spam(a)operamail.com> wrote:
> On 6/9/2010 9:54 AM, david wrote:
> > Hello,
> >
> > It seems that it is possible in fortran to give a function name
> > to a subroutine/function, to call it later.
> >
> > Is it possible to give a subroutine adresse to a
> > subroutine/function, to call it later ?
> >
> >
> > like in C:
> > void sub (void (*subroutine)(int param))
> > {
> > subroutine(1);
> > }
> >
> >
> >
> > Thanks for any tips
> >
> > david

> This has been possible for decades. See, for example, Section 5.12 of
> Metcalf, Reid and Cohen, Fortran 95/2003 Explained, OUP, 2004.

> If using Fortran 77, read about the EXTERNAL declaration. If using
> Fortran 9X, read about procedure arguments and interface declarations.

You are right, it is possible and I confused myself writing the
question. I'm sorry about that.

My real question: is it possible to store a "subroutine
address" into a variable to call it later.

- like in C :

/* subroutine address variable */
void (*subroutine_addr)(int param);

void sub2 (void)
{
subroutine_addr(1);
}

void sub (void (*subroutine)(int param))
{
subroutine_addr = subroutine;
sub2();
}

david
From: david on
Richard Maine <nospam(a)see.signature> wrote:
> david <deyv(a)free.fr> wrote:

> It sounds somewhat like you are asking for procedure pointers, which are
> new to the standard in f2003. Those do allow you to pointer assign a
> procedure in one place and then reference it in another. But they have
> nothing to do with confusing subroutines and functions.

You are right, I'm asking about procedure pointers !

Thanks very much, I did not go that far. I'll check the new
standards.

Sorry about asking confusing questions, I'll try to do better next
time.

david