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 compute the area of a triangle using
! a function.
REAL(kind=dp) :: b,h,a
! The name of the function must be declared in any routine
! that uses it.
REAL(kind=dp) :: areatri
WRITE(*,*) 'Input base and height'
READ(*,*) b,h
! To use the function, use an assignment statement just like if
! you were computing a value for sin, cos, etc.
a = areatri(b,h)
WRITE(*,*) 'area = ',a
END PROGRAM prog
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION areatri(base,height)
! The name of the function must follow the usual variable
! naming conventions
USE const
IMPLICIT NONE
! All dummy arguments to the function must have INTENT(IN)
! because the function should not be changing any of these
REAL(kind=dp),INTENT(IN) :: base,height
! The function must have a type (integer or double). This does
! not get an intent because it's not a dummy argument
REAL(kind=dp) :: areatri
! Finally, the name of the function is also the name of a variable
! in the body of the function. The function must assign a value
! to this variable.
areatri = base*height/2
END FUNCTION areatri