Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Allocatable arrays into Subroutine outside Argument list

Status
Not open for further replies.

mjodoin

Civil/Environmental
Jun 2, 2004
2
I am using Compaq Visual Fortran.

I need to pass an allocatable array into a subroutine. The trick is that I can not simply add it to the argument list because the subroutine is called from an IMSL routine which permits only 3 specific variables in the argument list. I don't think I can use a COMMON statement with allocatable arrays.

The only solution I have come up with is to 'over-define' the array rather than making it allocatable but this isn't very appealing. Any other ideas?
 
Replies continue below

Recommended for you

Allocate the array, pass it into the subroutine in the argument list, but in the subroutine dimension the array with a size of "1". So you want something like this:

CALL SUB1( ARG1, ARG2, XARRAY )
:
:

SUBROUTINE SUB1( ARG1, ARG2, XARRAY )
DIMENSION XARRAY(1)
:
:
RETURN
END

This should compile and run fine. Potential issues are:

1) You have to make sure you don't blow past the end of the array. The best thing to do here is put some type of marker in the last element of the array and test for it.

2) You won't be able to debug the array contents, since the compiler doesn't know how big the array is with the above dimensioning technique.



Richard Ay
COADE, Inc.
 
What Richard wrote will work. However... replace Xarray(1) with Xarray(*) to make your program standard conforming and more portable.

Now... if the array you want to pass to the sub has been allocated... you can pass it to the sub in the argument list. Of course... you say that the IMSL routine which calls the sub only has 3 arguments. I understand that. Also... you are correct that you can't place an allocated array in Common. Here is another solution for you.

I assume you are using an F90 or F95 compiler ( since you are working with allocatable arrays ). Thus... you have the ability to make "generic" subroutines which use the same name. Essentially, you make two subroutines with slightly different names. Then you use a "Module Procedure" statement to make those subs generic.

Module test

Interface Generic_Sub
Module Procedure Sub_1, Sub_2
End Interface Generic_Sub

Contains

Sub_1( x, y, z )
Integer :: x, y, z
::::::::
End Sub_1

Sub_2( x, y, z, a )
Integer :: x, y, z, a(*)
::::::::
End Sub_1
End Module test

Main Program
USE test, Only: Generic_Sub
:::::
Call Generic_Sub ( x, y, z ) ! IMSL callable
Call Generic_Sub ( x, y, z, a ) ! as You need it
End Program Main

Dan :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor