next up previous
Next: Typical Mistakes in using Up: CS101 Lecture 10 Previous: Fortran Function Syntax

Factorial Example

Integer Function Factorial(n)

  Implicit None
  Integer, Intent(IN) :: n  !formal input parameter
  Integer :: I, Ans         !local variables
   
  Ans = 1 
  DO I = 1, n
     Ans = Ans * I
   END DO 
  Factorial = Ans 
END FUNCTION Factorial

What is wrong with the following version?

Integer Function Factorial(n)
 Implicit None
 Integer, Intent(IN) :: n 
 Integer :: i
 Factorial = 1 
 DO i = 1, n
 Factorial = Factorial * i   ! the ``bug'' is here
 END DO 
END FUNCTION Factorial



G. Sivakumar 2000-08-23