What Is Actually Being Passed in a Fortran Sub Call?

  • Context: Fortran 
  • Thread starter Thread starter kubota
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The discussion clarifies how parameter passing works in Fortran, specifically in the context of a subroutine call. In the example provided, a two-dimensional array, Array1, is passed to a one-dimensional array, Z, while a one-dimensional array, Array2, is passed to an integer variable, L. Fortran utilizes "call by reference," meaning the addresses of the parameters are passed without type checking, allowing for flexible but potentially unsafe code usage. This behavior is common in older Fortran versions and is exemplified in libraries like Quadpack.

PREREQUISITES
  • Understanding of Fortran syntax and structure
  • Familiarity with array dimensions and memory storage in programming
  • Knowledge of "call by reference" parameter passing
  • Basic concepts of subroutines in Fortran
NEXT STEPS
  • Study "Fortran 77 parameter passing" for historical context
  • Explore "Fortran array storage order" to understand memory layout
  • Learn about "Fortran subroutine best practices" to avoid common pitfalls
  • Investigate "Quadpack library usage" for practical applications of Fortran subroutines
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers maintaining legacy Fortran code, and anyone interested in understanding the intricacies of parameter passing in Fortran subroutines.

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.
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
5
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
13K