Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28591 Discussions

How can I use INTERFACE for an array that does not start with 1?

marshall-l-buhl
Beginner
499 Views

Hi,

I'm trying to pass an allocatable array to a function and am using INTERFACE so I do not have to pass the lower and upper limits of the array.  The array starts at 0 instead of 1 and I cannot get it to work.  I get the run-time error: "Subscript #1 of the array COEFS has value 0 which is less than the lower bound of 1."  Here is a sample program:

PROGRAM Test_Interface

   IMPLICIT                  NONE

   INTERFACE
      FUNCTION Poly ( Val, Coefs )
         REAL, INTENT(IN) :: Coefs  (:)          ! An array of polynomial coefficients in Poly().
         REAL, INTENT(IN) :: Val                 ! The value to compute the polynomial for.
      END FUNCTION Poly   
   END INTERFACE          

   REAL, ALLOCATABLE      :: MyCoefs (:)         ! A local array.

   ALLOCATE ( MyCoefs( 0:2 ) )

   MyCoefs(0) = 0.0
   MyCoefs(1) = 1.0
   MyCoefs(2) = 2.0

   PRINT *, 'Poly = ', Poly( 1.0, MyCoefs )

END PROGRAM Test_Interface
REAL FUNCTION Poly( Val, Coefs ) RESULT( Res )

   REAL, INTENT(IN)      :: Coefs(:)
   REAL, INTENT(IN)      :: Val

   INTEGER               :: I

   Res = 0.0

   DO I=0,SIZE( Coefs )
      Res = Res + Coefs(I)*Val**I
   ENDDO ! I

   RETURN
END FUNCTION Poly ! ( Val, Coefs )

Yes, I know how to get around the problem by starting the array at 1 or passing the array bounds, but I would rather not do that.  Advice would be greatly appreciated.

Marshall

0 Kudos
3 Replies
marshall-l-buhl
Beginner
499 Views

Oops!  I suppose that DO loop should be:

DO I=0,SIZE( Coefs )-1

0 Kudos
Steven_L_Intel1
Employee
499 Views

This is the way Fortran defines things for an assumed-shape array dummy argument.  The lower bound is 1 and the upper bound is the extent of the array.  You could declare the dummy argument as (0:) and that might do what you want. What you can't do is have the lower bound passed in automatically.

0 Kudos
marshall-l-buhl
Beginner
499 Views

Steve,

You have come through for me once again.  That worked.

I really do appreciate all the help you have given me over the years.  Tell you boss I think you deserve a raise!  :)

Marshall

0 Kudos
Reply