[Fortran] Passing elements of array to subroutine

In summary, Fortran uses the "pass-by-reference" method to pass elements of an array to a subroutine. This means that the actual array elements are not copied, but rather a reference to the memory location of the elements is passed to the subroutine. It is possible to pass a single element of an array to a subroutine in Fortran, but it must still be passed by reference and the subroutine must be able to handle a single element as its argument. To pass an entire array to a subroutine, you need to use the "pass-by-reference" method and specify the array as the argument in the subroutine declaration. You can also specify the dimensions of the array in the declaration to ensure that the subroutine can handle the correct size and shape of
  • #1
VasanthG
13
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
  • #2


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


double post
 
Last edited:
  • #4


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.
 
  • #5


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.
 
  • #6


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

FAQ: [Fortran] Passing elements of array to subroutine

What is Fortran's method for passing elements of an array to a subroutine?

Fortran uses the "pass-by-reference" method to pass elements of an array to a subroutine. This means that the actual array elements are not copied, but rather a reference to the memory location of the elements is passed to the subroutine.

2. Can I pass a single element of an array to a subroutine in Fortran?

Yes, it is possible to pass a single element of an array to a subroutine in Fortran. However, the element must still be passed by reference, and the subroutine must be able to handle a single element as its argument.

3. How can I pass an entire array to a subroutine in Fortran?

To pass an entire array to a subroutine in Fortran, you need to use the "pass-by-reference" method and specify the array as the argument in the subroutine declaration. You can also specify the dimensions of the array in the declaration to ensure that the subroutine can handle the correct size and shape of the array.

4. Can I modify the elements of an array within a subroutine in Fortran?

Yes, you can modify the elements of an array within a subroutine in Fortran. Since the elements are passed by reference, any changes made to the elements within the subroutine will also be reflected in the original array.

5. Are there any restrictions on the data types of arrays that can be passed to a subroutine in Fortran?

There are no specific restrictions on the data types of arrays that can be passed to a subroutine in Fortran, as long as the data type is supported by the language. However, it is important to ensure that the data type of the array in the subroutine declaration matches the data type of the array being passed.

Similar threads

Replies
59
Views
10K
Replies
3
Views
2K
Replies
5
Views
3K
Replies
5
Views
7K
Replies
4
Views
2K
Replies
25
Views
2K
Replies
8
Views
4K
Back
Top