PROGRAM Reversing_a_List
!------------------------------------------------------------------
! Driver program to test the function subprogram Reverse.  It reads
! a list of numbers Item(1), Item(2), ..., Item(NumItems), calls
! Reverse to contruct the list in reverse order, and displays the
! reversed list.  Identifiers used are:
!   NumItems  : number of items (constant)
!   Item      : one-dimensional array of items
!   Reverse   : function to reverse an array
!
! Input:  NumItems and a list of NumItems integers
! Output: The reversal of the list
!------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER, PARAMETER :: NumItems = 10
  INTEGER, DIMENSION (NumItems) :: Item

  WRITE (*, '(1X, A, I3, A)', ADVANCE = "NO") &
        "Enter", NumItems, " integers: "
  READ *, Item

  PRINT *, "The reversal of this list is:"
  PRINT *, Reverse(Item)
  STOP

CONTAINS

  ! Reverse--------------------------------------------------------
  ! External function to find the reversal of an array X. Local 
  ! variables used are:
  !   NumElements : number of elements in X
  !   I           : subscript
  !
  ! Accepts:  Assumed-shape array X
  ! Returns:  The reversal of X
  !----------------------------------------------------------------
  
  FUNCTION Reverse(X)
  
    IMPLICIT NONE
    INTEGER, DIMENSION(:), INTENT(IN) :: X
    INTEGER, DIMENSION(SIZE(X)) :: Reverse
    INTEGER :: NumElements, I
  
    NumElements = SIZE(X)
    DO I = 1, NumElements
       Reverse(I) = X(NumElements - I + 1)
    END DO
    RETURN

  END FUNCTION Reverse

END PROGRAM Reversing_a_List
