! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module F01M
    implicit none
    public :: A
  contains
    function A( N, B ) result( A_R )
      integer, intent (in) :: N   ! Argument
      real, dimension(:), intent (in) :: B   ! Assumed-shape argument array
      real, dimension(size( B )) :: A_R   ! Automatic-shape result array
      real, dimension (0: N - 1) :: Y   ! Automatic-shape local array
      integer, parameter :: K = 7
      real, dimension(K) :: X  ! Fixed-size local array
      real, dimension(:), allocatable :: Z   ! Deferred-shape local array
      integer :: I  ! I is an ACID variable
    ! start function A
      X = 0.0
      Y = (/ (I, I = 1, N) /)   ! Array constructor: see Sec. 5.5
      allocate( Z(size( B )) )
      Z = 0.0
      Z(: N) = B(: N) - Y
      A_R = Z - 1.0
      X = B(: min( size( B ), K ))
      write (unit = *, fmt = *) " N, size( B ): ", N, size( B )
      write (unit = *, fmt = *) " B: ", B
      write (unit = *, fmt = *) " Y: ", Y
      write (unit = *, fmt = *) " X: ", X
      write (unit = *, fmt = *) " Z: ", Z
      deallocate( Z )
      return
    end function A
  end module F01M
