What Is Actually Being Passed in a Fortran Sub Call?

  • Fortran
  • Thread starter kubota
  • Start date
  • Tags
    Fortran
In summary, the code is passing the address of the first element of Array1 (containing the values for err1 and err2) to the subroutine, and the address of the second element of Array2 to the subroutine.
  • #1
kubota
4
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
  • #2


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!
 
  • #3


AlephZero,

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

With my thanks.
 

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

1. What is a Fortran Sub Call?

A Fortran Sub Call is a statement in a Fortran program that invokes a subroutine or function. It is used to transfer control from the main program to a subroutine, which performs a specific task and then returns control back to the main program.

2. What is passed in a Fortran Sub Call?

In a Fortran Sub Call, the arguments or parameters are passed from the main program to the subroutine. These can be variables, expressions, or arrays that the subroutine will use in its calculations or operations.

3. How are arguments passed in a Fortran Sub Call?

Arguments in a Fortran Sub Call can be passed by value or by reference. When passed by value, the value of the argument is copied and passed to the subroutine. When passed by reference, the address of the argument is passed, allowing the subroutine to access and modify the original value.

4. Can multiple arguments be passed in a Fortran Sub Call?

Yes, multiple arguments can be passed in a Fortran Sub Call. The arguments are separated by commas and their order must match the order in which they are declared in the subroutine.

5. What is the difference between a subroutine and a function in Fortran?

A subroutine performs a specific task and then returns control back to the main program, while a function returns a value to the main program. Functions are typically used for mathematical calculations, while subroutines are used for more general tasks.

Similar threads

  • Programming and Computer Science
Replies
4
Views
646
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top