The FORTRAN Program: tpk.f95

  1 !  tpk.f95:  Knuth's TPK program in Fortran 95 (compiled by NAG F95)
  2 
  3 program TPK
  4 
  5    implicit none       ! no implicitly declared variables!
  6    integer :: i
  7    real :: y
  8    real, dimension (0:10) :: A   !  declare array "A" elements 0..10
  9 
 10    ! Read in the values of the array "A"
 11    read *, A           ! take advantage of an implicit loop
 12 
 13    ! In reverse order, apply "f" to each element of "A" and print
 14    do i = UBOUND(A,dim=1), LBOUND(A,dim=1), -1
 15       y = f (A(i))
 16       if (y > 400.0) then
 17           print *, " too large"
 18       else
 19           print *, y
 20       end if
 21    end do
 22 
 23 contains
 24 
 25    ! f(x) = sqrt(|x|) + 5*x**3
 26    function f (x); real f
 27       real, intent (in) :: x  ! "x" is an 'in' parameter of type real
 28       f = sqrt (abs(x)) + 5.0*x**3
 29    end function f
 30 
 31 end program TPK