Implementing Recursion Function w/o FORTRAN 90 Built-in

  • Thread starter Thread starter Sue Parks
  • Start date Start date
  • Tags Tags
    Function Recursion
Click For Summary
The discussion centers on implementing a factorial function in FORTRAN 90 without using its built-in recursion feature. Participants highlight the need for a return statement in the recursive function to avoid errors, specifically mentioning that the function must have an explicit return type. There are concerns about the potential for large factorial values exceeding the limits of double precision representation. Additionally, a suggestion is made to replace the word "CONTINUE" with "CONTAINS" for clarity in the program structure. The conversation emphasizes the importance of correctly defining the function and managing data types to ensure accurate calculations.
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 bigfooted
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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