replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Recursion in Fortran 77?
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Recursion in Fortran 77?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Thu Feb 24, 2011 8:42 pm    Post subject: Recursion in Fortran 77? Reply with quote

You can't use recursion in Fortran 77, but it's part of Fortran 90 onwards.

Nonsense of course! Smile

The code below is standard Fortran 77 and it does recursion. I show this only as a "quirky" example. I wouldn't suggest you use this in real code.

Code:

      PROGRAM RECURSE

C     An example of legal recursion in Fortran 77

      INTEGER M,N,FACT
      EXTERNAL FACT

C     Calculate a Factorial using recursion. Of course you can't
C     make N too big or you will get an overflow.
C     Note that a pointer to FACT is passed to function FACT.
C     This is the "Cunning Plan" as Baldrick would say.

      N = 5
      M = FACT(N,FACT)

      PRINT *,'FACTORIAL OF ',N,' IS ', M
      END

      FUNCTION FACT(N, FUNC)
      INTEGER FACT, N, FUNC
      EXTERNAL FUNC
      IF (N .EQ. 0) THEN
         FACT = 1
      ELSE
         FACT = N * FUNC(N-1, FUNC)
      ENDIF
      END
Back to top
View user's profile Send private message
Robert



Joined: 29 Nov 2006
Posts: 457
Location: Manchester

PostPosted: Sat Feb 26, 2011 11:23 am    Post subject: Reply with quote

It might work - but is it standard Fortran 77? I wonder if there are compilers out there that plant code to prevent this sort of thing. FTN95 allows it (as you know).
Back to top
View user's profile Send private message Visit poster's website
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Sat Feb 26, 2011 1:12 pm    Post subject: Reply with quote

Hi Robert.

It is standard Fortran 77 AFAIK. And I have tried it on a lot of Fortran compilers (ifort, gfortran, g95, open64 ... ) (enforcing Fortran 77 compliance where I can).

It works with Silverfrost FTN95 and it works if you tick the box under options that says "Use FTN77 instead of FTN95". However, I'm not sure what the latter does as there is no FTN77 executable in the Silverfrost install directory.

The "technique" isn't of much practical use as you can't use local variables in the function being called. Since you cannot guarantee these will be created on the stack with Fortran 77. For example, if I modify the code as follows, it doesn't always work.

Code:

      FUNCTION FACT(N, FUNC)
      INTEGER FACT, N, FUNC
      EXTERNAL FUNC
      INTEGER M
      IF (N .EQ. 0) THEN
         FACT = 1
      ELSE
         M = N
         FACT = M*FUNC(N-1, FUNC)
      ENDIF
      END


This is really an example of how Fortran 77 is broken, since you can put together legal code that doesn't work. In Fortran 90 and higher you have to explicitly use RECURSIVE to ensure the stack is used and ensuring the resulting code is portable. (C, C++ etc use "auto" variables by default so recursion is allowed by default.)
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Sat Mar 05, 2011 5:12 pm    Post subject: Re: Recursion in Fortran 77? Reply with quote

Quote:
You can't use recursion in Fortran 77, but it's part of Fortran 90 onwards.

Nonsense of course! Smile

The code below is standard Fortran 77 and it does recursion. I show this only as a "quirky" example. I wouldn't suggest you use this in real code.
[Code removed]


Being able to get code past the compiler is not proof of the legality of the code.

Section 15.2 of the Fortran 77 Standard says: "An external function specified by a function subprogram may be referenced within any other procedure subprogram or the main program of the executable program. A subprogram must not reference itself, either directly or indirectly. "

As with most violations of the standard, the compiler is not required by the Standard to catch violations of the rule against recursion.

Some compilers provided for recursion as an extension, either by the specification of an option to the compiler or by the RECURSIVE keyword in the code.
Back to top
View user's profile Send private message
BrianS



Joined: 17 May 2011
Posts: 1

PostPosted: Tue May 17, 2011 9:05 pm    Post subject: Reply with quote

This type of construct works on some Fortran-77 compilers, not others. If the compiler allocates local storage off of the stack, it will probably work. If the compiler allocates static storage for locals in a routine, it will clobber them. It will run faster and produce more compact code.

You can pull a lot of tricks in Fortran-77 if you know how the compiler generates code.
Back to top
View user's profile Send private message
GoldenHeat



Joined: 13 Jul 2011
Posts: 6

PostPosted: Thu Jul 14, 2011 9:13 pm    Post subject: Re: Reply with quote

BrianS wrote:
This type of construct works on some Fortran-77 compilers, not others. If the compiler allocates local storage off of the stack, it will probably work. If the compiler allocates static storage for locals in a routine, it will clobber them. It will run faster and produce more compact code.

You can pull a lot of tricks in Fortran-77 if you know how the compiler generates code.


Stupid question, but is there a good guide for teaching myself about the compiler and video converter? I have been running an email marketing campaign and am starting realize that i don't exactly know enough about the things I am trying to inform the audience of. Just a simple link or instructions would be greatly appreciated.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group