Implementing Recursion Function w/o FORTRAN 90 Built-in

  • Context: Fortran 
  • Thread starter Thread starter Sue Parks
  • Start date Start date
  • Tags Tags
    Function Recursion
Click For Summary

Discussion Overview

The discussion revolves around implementing a recursive factorial function in FORTRAN 90 without utilizing the built-in recursion capabilities. Participants explore various coding approaches, syntax issues, and potential pitfalls related to recursion and data types.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions how to implement a factorial function without the built-in recursion function in FORTRAN 90.
  • Another participant suggests the need for a return statement in the Factorial function after the IF statement.
  • A participant reports encountering errors when trying to implement the return statement, expressing uncertainty about the necessity of using a recursive function.
  • Concerns are raised about the potential for factorial values to exceed the limits of double precision numbers.
  • There are suggestions regarding the correct use of the CONTINUE statement, with one participant proposing it should be replaced with CONTAINS.
  • A later reply offers an alternative structure for the program, maintaining the recursive function while addressing previous syntax issues.

Areas of Agreement / Disagreement

Participants express differing opinions on the necessity of certain statements and the structure of the program. There is no consensus on the best approach to implement the recursive function, and multiple viewpoints on syntax and functionality remain unresolved.

Contextual Notes

Participants highlight limitations related to the handling of large factorial values and the implications of using different data types, but these issues are not fully resolved within the discussion.

Sue Parks
Messages
38
Reaction score
0
I understand that FORTRAN 90 has a built-it recursion function. How could I implement this function without using the built in recursion function?

Fortran:
PROGRAM RECURSIVE_FACT
    IMPLICIT NONE
    INTEGER, PARAMETER :: M = 100
    DOUBLE PRECISION :: fact
    INTEGER :: I

    
   PRINT *, factorial (N)
        CONTINUE
    RECURSIVE FUNCTION Factorial(n)  RESULT(Fact)

        IMPLICIT NONE
        INTEGER :: Fact
        INTEGER, INTENT(IN) :: n

        IF (n == 0) THEN
              Fact = 1
        ELSE
              Fact = n * Factorial(n-1)
        END IF

        END FUNCTION Factorial

END PROGRAM RECURSIVE_FACT
 
Last edited by a moderator:
Technology news on Phys.org
Don't you need a return statement following the IF statement in your Factorial function?

Fortran:
       IF ( n == 0 ) THEN
              Fact =1
       ELSE
              Fact = n * Factorial( n - 1 )
       ENDIF

       RETURN Fact

END FUNCTION Factorial
 
WHE I FIRST TRIED YOUR SUGGESTION (RETURN STATEMENT), IT RETURNED 0. Now I have multiple errors: (Here is one). Do I HAVE to use the recursive function?
RECURSIVE_FACTORIAL.f90:20.26:
Function 'factorial' at (1) has no IMPLICIT type

Fortran:
IMPLICIT NONE
        INTEGER, PARAMETER :: M = 100
          DOUBLE PRECISION :: fact
        INTEGER :: I,N

     
     
        PRINT *, factorial (N) 
        CONTINUE
    RECURSIVE FUNCTION Factorial(n)  RESULT(Fact)   ! CAN I MAKE MY OWN FUNCTION HERE??

        IMPLICIT NONE
        DOUBLE PRECISION :: Fact
        INTEGER, INTENT(IN) :: n

        IF (n == 0) THEN
              Fact = 1
        ELSE
              Fact = n * Factorial(n-1)
        END IF
        RETURN FACT

        END FUNCTION Factorial

 END PROGRAM RECURSIVE_FACTORIAL
 
the word CONTINUE , should really be CONTAINS.
 
Also, there was another thread where we discussed the fact that the factorial can get really large and may end up not fitting even in a double precision number...
 
gsal said:
the word CONTINUE , should really be CONTAINS.
Or you could write the program like this:
Fortran:
PROGRAM RECURSIVE_FACT
    IMPLICIT NONE
    INTEGER, PARAMETER :: M = 100
    DOUBLE PRECISION :: fact
    INTEGER :: I
   
    PRINT *, factorial (N)
END PROGRAM RECURSIVE_FACT

RECURSIVE FUNCTION Factorial(n)  RESULT(Fact)
     IMPLICIT NONE
     INTEGER :: Fact
     INTEGER, INTENT(IN) :: n

     IF (n == 0) THEN
           Fact = 1
     ELSE
           Fact = n * Factorial(n-1)
     END IF
END FUNCTION Factorial
 
  • Like
Likes   Reactions: bigfooted

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
23K
Replies
11
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K