Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] Passing elements of array to subroutine

  1. Jul 20, 2012 #1
    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.
     
  2. jcsd
  3. Jul 20, 2012 #2

    uart

    User Avatar
    Science Advisor

    Re: Passing elements of array to subroutine

    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 in to it in the subroutine.
     
    Last edited: Jul 20, 2012
  4. Jul 20, 2012 #3

    uart

    User Avatar
    Science Advisor

    Re: Passing elements of array to subroutine

    double post
     
    Last edited: Jul 20, 2012
  5. Jul 20, 2012 #4
    Re: Passing elements of array to subroutine

    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.
     
  6. Jul 20, 2012 #5

    uart

    User Avatar
    Science Advisor

    Re: Passing elements of array to subroutine

    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 (Text):

    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.
     
  7. Jul 20, 2012 #6
    Re: Passing elements of array to subroutine

    Thank you so much for your time and help. I'll implement it in my code and check.
     
  8. Jul 20, 2012 #7

    jtbell

    User Avatar

    Staff: Mentor

    Thread title edited.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook