- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oops! I suppose that DO loop should be:
DO I=0,SIZE( Coefs )-1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page