Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What Is Actually Being Passed in a Fortran Sub Call?

  1. Jan 31, 2012 #1
    What Is Actually Being Passed in a Fortran Sub Call??

    I'm trying to understand exactly what is being passed from a calling routine to a called routine in an older Fortran program. Perhaps if I could present a simple example to illustrate my dilemma:

    Say the calling routine includes this code:

    Real Array1
    Integer Array2
    Dimension Array1(20,100), Array2(100)
    which established Array1 as a two-dimension Real array of size 20x100, and Array2 as a single-dimension Integer array of size 100. So far so good.

    A call to a subroutine is then made:

    CALL SUB1(Array1(1,err1), Array2(err2))
    where err1 and err2 are some index values.

    Now, the Subroutine is declared as:

    Real Z
    Integer L
    Dimension Z(100)
    where Z is a Real array of size 100, and L is an Integer variable.

    Thus we have a two-dimension array, Array1, being passed to a single-dimension array, Z, and a single-dimension array, Array2, being passed to an integer variable L.

    I do not understand how such code is able to successfully work, nor can I determine exactly what is being passed to/received by the called sub.

    BTW, this type of code usage is used in Quadpack, so I presume the code does operate correctly.

    I'm hoping someone can advise me how this Fortran code is able to work. I haven't been able to find this information to date, nor in any books I have.

    TIA for any assistance.
  2. jcsd
  3. Jan 31, 2012 #2


    User Avatar
    Science Advisor
    Homework Helper

    Re: What Is Actually Being Passed in a Fortran Sub Call??

    Fortran passes the address of the parameter. In "old" versions of Fortran there is no check that the attributes of the parameter is compatible with what the subroutine requires.

    Arrays are stored column-wise, i.e. in memory you have Array1(1,1) Array1(2,1), ... Array1(20,1), Array1(1,2), Array1(2,2) .... Array1(19,100), Array1(20,100).

    So CALL SUB1(Array1(1,err1), Array2(err2)) is passing the address of the first element of column "err1" of Array1, and the address of element "err2" of Array2.

    The subroutine is then using the column of Array1 as a 1-dimensional array, and the element from Array2 as an integer. Fortran parameters are "call by reference", so SUB1 is accessing the storage within Array1 and Array2 directly, not working with a copy of the data (as in languages like C, etc)

    This type of parameter passing is very common in Fortran code. This is a straightforward example, but given the rules above, you can do things that are much more "creative" (or abusive!) if you want to!
  4. Jan 31, 2012 #3
    Re: What Is Actually Being Passed in a Fortran Sub Call??


    I'm most grateful for your detailed, clear response. I now understand what the code is doing.

    With my thanks.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook