[Fortran] Passing elements of array to subroutine

Click For Summary

Discussion Overview

The discussion revolves around the syntax and semantics of passing arrays to subroutines in Fortran, specifically addressing memory allocation, parameter passing methods, and handling rank mismatches. Participants share their experiences and seek clarification on best practices for using subroutines with arrays.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant inquires about the correct syntax for passing an array to a subroutine and returning results, expressing confusion over memory allocation.
  • Another participant clarifies that Fortran passes parameters by reference by default, suggesting that changes made in the subroutine will reflect in the main program without needing to allocate memory for the dummy variables.
  • A later reply emphasizes the need to define the array in the subroutine and notes that modifications to the array in the subroutine will affect the original array in the main program.
  • There is mention of potential errors related to rank mismatch when not allocating memory for dummy variables, indicating a common issue faced by those new to subroutines.
  • One participant notes that whether Fortran passes by reference or uses "copyback" is implementation dependent, adding a layer of complexity to the discussion.

Areas of Agreement / Disagreement

Participants generally agree on the principle that Fortran passes arrays by reference, but there are differing views on the necessity of memory allocation and the implications of rank mismatches. The discussion remains unresolved regarding the best practices for handling these issues.

Contextual Notes

Some participants express uncertainty about the correct handling of array dimensions and memory allocation in subroutines, indicating a need for further clarification on these technical aspects.

VasanthG
Messages
13
Reaction score
0
I have one main program where I have calculated some array, say A(i,j)
If I want ot use the values in subroutine, and calculate some more values and give back the result to main program, how my syntax should be?
I tried this way. Its not working.
program a
......
call sub(A,B,C)
end program a

subroutine (X,Y,Z)
allocated memory same as A,B,C to X,Y,Z
some formulae
return
end subroutine sub

Thank you.
 
Technology news on Phys.org


Hi Vasanth, it's a good idea to specify what programming language you are using when asking a question. But let me guess it's fortran.

By default Fortran passes all parameters by "reference". This means that when you pass the array to the subroutine it merely passes a reference to the actual same array in the main program. So any changes made in the subroutine are automatically reflected in the variable back in the main program. Also there is no need to allocate memory into it in the subroutine.
 
Last edited:


double post
 
Last edited:


Thank you so much. Yeah I am using FORTRAN.. sorry I missed to mention.
say do 10 i=1,10
do 10 j=1,10
A(i,j)=i+j
10 continue

This 10 cross 10 values must go to subroutine for a formula which uses
say g(i,j)=A(i,j)+2 and my o/p should be values of g(i,j)

For this case, if I am not allocating the memory for dummy variable, its showing errors of rank mismatch.
I am new to subroutines. If I am doing basic mistakes, pls correct me.
Thanks again.
 


VasanthG said:
Thank you so much. Yeah I am using FORTRAN.. sorry I missed to mention.
say do 10 i=1,10
do 10 j=1,10
A(i,j)=i+j
10 continue

This 10 cross 10 values must go to subroutine for a formula which uses
say g(i,j)=A(i,j)+2 and my o/p should be values of g(i,j)

For this case, if I am not allocating the memory for dummy variable, its showing errors of rank mismatch.
I am new to subroutines. If I am doing basic mistakes, pls correct me.
Thanks again.

Yes you still need to define the array in the subroutine, but any modifications to that array will automatically be reflected as changes to the original array in the main program.

Code:
program test
  implicit none
  integer a(20)
  ...
  call example(a)
  ...

contains
subroutine example(x)
 implicit none
 integer x(20)
 ...           ! Changes to x() here cause changes in a() in the main program
end subroutine example
end program test
BTW. Whether or not it actually passes by reference or just uses "copyback" is implementation dependent. But in any case the "side effect" on "x" in the subroutine will change the values of "a" in the main.
 


Thank you so much for your time and help. I'll implement it in my code and check.
 
Thread title edited.
 

Similar threads

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