MODULE const INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0) REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0 END MODULE const ! PROGRAM prog USE const IMPLICIT NONE ! ! Program to implement the factorial as a function ! instead of a subroutine INTEGER :: n REAL(kind=dp) :: factvalue ! Declare the factfun as a double value in the calling routine. REAL(kind=dp) :: factfun WRITE(*,*) 'Input n' READ(*,*) n ! Invoke the facfun function using an assignment statement factvalue = factfun(n) WRITE(*,*) 'factvalue = ',factvalue END PROGRAM prog !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FUNCTION factfun(n) ! ! Function for computing n! ! USE const IMPLICIT NONE INTEGER,INTENT(IN) :: n ! Give the function a type (double in this case) REAL(kind=dp) :: factfun INTEGER :: i ! The name of the function is also a variable in the function ! body. The function must assign a value to factfun factfun = 1 DO i = 1,n factfun = factfun*i ENDDO END FUNCTION factfun