Silverfrost Forums

Welcome to our forums

Recursion in Fortran 77?

24 Feb 2011 7:42 #7834

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

Nonsense of course! 😃

The code below is **standard Fortran 77 ** and it [u:77a59d0c3b]does recursion[/u:77a59d0c3b]. I show this only as a 'quirky' example. I wouldn't suggest you use this in real 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

26 Feb 2011 10:23 #7842

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).

26 Feb 2011 12:12 #7843

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.

      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.)

5 Mar 2011 4:12 #7874

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

Nonsense of course! 😃

The code below is **standard Fortran 77 ** and it [u:7cc807d55c]does recursion[/u:7cc807d55c]. 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.

17 May 2011 8:05 #8268

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.

14 Jul 2011 8:13 #8576

Quoted from BrianS 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.

Please login to reply.