Implementing Recursion Function w/o FORTRAN 90 Built-in

  • Thread starter Thread starter Sue Parks
  • Start date Start date
  • Tags Tags
    Function Recursion
AI Thread 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
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Back
Top