×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Passing Allocatable arrays into Subroutine outside Argument list

Passing Allocatable arrays into Subroutine outside Argument list

Passing Allocatable arrays into Subroutine outside Argument list

(OP)
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?

RE: Passing Allocatable arrays into Subroutine outside Argument list

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.

RE: Passing Allocatable arrays into Subroutine outside Argument list

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   
www.dtware.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close