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

  • Thread starter Thread starter kubota
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
In Fortran, when a subroutine is called, the parameters are passed by reference, meaning the address of the variable is sent rather than a copy of the data. In the example discussed, a two-dimensional array, Array1, is passed to a subroutine as a one-dimensional array, Z, and a one-dimensional array, Array2, is passed as an integer variable, L. The subroutine accesses the memory directly, treating the specified column of Array1 as a one-dimensional array and using the specific element from Array2 as an integer. This behavior is typical in older Fortran versions, where there is no type checking for parameter compatibility, allowing for flexible but potentially risky coding practices. Understanding this mechanism clarifies how the code operates correctly despite apparent mismatches in dimensions.
kubota
Messages
4
Reaction score
0
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:

SUBROUTINE SUB1(Z, L)
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.
 
Technology news on Phys.org


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!
 


AlephZero,

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

With my thanks.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top