[comp.lang.fortran] Recursion in Fortran

lotto@lamia.harvard.edu (Jerry Lotto) (06/06/90)

I would appreciate pointers to any people or documentation that might
help me work out a problem.  I am not really trying to do general
recursion... there is a single calling stack that may need to repeat
once within its own context.  The problem I am having is with calling
args -> ie:

A -> B(foo1, foo2) -> C(var1, var2) -> D(var1, var2) -> E ->
     B(bar1, bar2) -> C(var1, var2) -> D(var1, var2)

Everything is fine until we return from E.  Now the addresses D\var1
and D\var2 are screwed up (the same as E\bar1 and E\bar2) whereas the
addresses of C\var1, C\var2, B\var1, and B\var2 are correct (that of
A\foo1 and A\foo2).  I would like to protect the argument stacks so
that all of the variables are properly restored upon return from E.
Any help would be greatly appreciated.  AdThanksvance.

--
Jerry Lotto <lotto@lhasa.harvard.edu>      "Have you hugged the curves today?"
Chemistry Dept., Harvard Univ.              AMA #520019 DoD #018 HOG #0323880

bethge@wums2.wustl.edu (Paul H. Bethge) (06/13/90)

VMS Fortran (like  most  Fortrans)  does  *not*  in  general  support
recursion, although it sometines works for simple cases.  The problem
is that for some kinds of arguments, a temporary copy is made of  the
argument  address,  and  the  argument is addressed via the temporary
rather than via the argument list.  Other  temporaries  may  also  be
generated,  depending  on  the  code.  The temporaries are statically
allocated, not on the stack, so they get overwritten if  the  routine
is called recursively.  There is not much you can do about this.

_____________________________________________________________________
Paul H. Bethge                                  bethge@wums.wustl.edu
Biochemistry, Box 8231                             bethge@wums.bitnet
Washington University
St. Louis, MO 63110                                      314-362-3354

khb@chiba.Eng.Sun.COM (Keith Bierman - SPD Advanced Languages) (06/13/90)

In article <3661.267509f4@wums2.wustl.edu> bethge@wums2.wustl.edu (Paul H. Bethge) writes:


   VMS Fortran (like  most  Fortrans)  does  *not*  in  general  support
   recursion, although it sometines works for simple cases.  The problem
....


I think that *most* fortrans probably do. Unix platforms usually do,
and some of the PC  compilers allow it also. Between MSDOS and Unix
you have *most* if you are counting heads or vendors.

cheers (from the uk)


--
Keith H. Bierman    |*My thoughts are my own. !! kbierman@Eng.Sun.COM
It's Not My Fault   | MTS --Only my work belongs to Sun* khb@chiba.Eng.Sun.COM
I Voted for Bill &  | Advanced Languages/Floating Point Group (415 336 2648)   
Opus                | "When the going gets Weird .. the Weird turn PRO"

seymour@milton.acs.washington.edu (Richard Seymour) (06/14/90)

In article <KHB.90Jun13075104@chiba.Eng.Sun.COM> khb@chiba.Eng.Sun.COM (Keith Bierman - SPD Advanced Languages) writes:
>In article <3661.267509f4@wums2.wustl.edu> bethge@wums2.wustl.edu (Paul H. Bethge) writes:
>>   VMS Fortran (like  most  Fortrans)  does  *not*  in  general  support
>>   recursion, although it sometines works for simple cases.  The problem
>>....
>I think that *most* fortrans probably do. Unix platforms usually do,
>and some of the PC  compilers allow it also. Between MSDOS and Unix
>you have *most* if you are counting heads or vendors.

Just because lots of people do it, does not mean it's correct.
To Quote ANSI x3.9-1978 Fortran 77 Standard sect 15.6.2.2 (page 15-10)
(both subset and full standard):

  "A subprogram must not reference itself, either directly or indirectly."

That's MUST NOT.  and "indirectly" can be loosely translated to: by
 sneaky means such as calling something which calls the caller.

(Raytheon's Fortran for their 704 computer (in 1972 or thereabouts)
 had a /r switch to allow recursion.  Just because i used it did not
 mean that it was a good idea (and it really confused other people)).
--dick

mikel@pyrps5 (Mike Lipsie) (06/14/90)

In article <4108@milton.acs.washington.edu> seymour@milton.u.washington.edu (Richard Seymour) writes:
>
>  "A subprogram must not reference itself, either directly or indirectly."
>
>That's MUST NOT.  and "indirectly" can be loosely translated to: by
> sneaky means such as calling something which calls the caller.

It seems that it is time yet again to remind the readers of this group
of a feature of the Fortran standard that is a bit different than those
of other languages.

The standard is for Fortran _programs_, not compilers.  A correct
compiler is one that will compile a correct program.

In the quote above, any program that does call itself (directly
or indirectly) is not a correct Fortran 77 program.  A compiler
is allowed to do anything it wants to an incorrect program,
including letting it work or letting it fail in a quiet, insidious
manner.

-----------
Mike Lipsie                               mikel@pyramid.com
Pyramid Technology Corp, Mountain View, CA  +1 415 335 8657
"Toto kansasoseum non est cognito" -- Phil Frank