Implementing Recursion Function w/o FORTRAN 90 Built-in

In summary, the conversation was about implementing a recursive function in FORTRAN 90 without using the built-in recursion function. The suggested solution was to use the return statement and to be careful about the potential for large factorial numbers. The correct code was provided and the conversation also touched on the use of "contains" instead of "continue" and the potential issue with large numbers in the factorial function.
  • #1
Sue Parks
38
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
  • #2
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
 
  • #3
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
 
  • #4
the word CONTINUE , should really be CONTAINS.
 
  • #5
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...
 
  • #6
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

1. How can recursion be implemented without using FORTRAN 90 built-in functions?

Recursion can be implemented in FORTRAN 90 by using a combination of subroutines and functions. The subroutine will contain the recursive call, while the function will serve as the base case and return the final value.

2. What is the difference between recursive and iterative functions?

The main difference between recursive and iterative functions is that recursive functions call themselves, while iterative functions use loops to repeat a set of instructions until a certain condition is met. Recursive functions can be more elegant and concise, but they may also be less efficient in terms of memory usage.

3. How can a recursive function be optimized for efficiency?

To optimize a recursive function, one can use tail recursion, which eliminates the need for the computer to store intermediate values in memory. This can greatly improve efficiency and prevent potential stack overflow errors.

4. Can recursion be used for any type of problem?

Recursion can be used for a wide range of problems, but it may not always be the most efficient or practical solution. It is best suited for problems that can be broken down into smaller, similar subproblems.

5. Are there any limitations to using recursion in programming?

One limitation of using recursion is that it can potentially lead to stack overflow errors if the function calls itself too many times. Additionally, recursive functions may not be well-suited for problems that require a lot of memory or have time constraints.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
619
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
22K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top