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

How to pass array element form a subroutine to main function

  1. Sep 14, 2010 #1

    FZK

    User Avatar

    Hi guys.

    I want to transfer each elements of an array (which is declared in one subroutine)to main function using common block technique.
    When i am doing so i go always a constant value in the array in the main function.

    Suppose i have one array bparz(i) which has different values in one subroutine for all the values of i, but when i want to transfer the whole array to main function using common block then it gives only constant value for all values of i.
    So if anybody has some guidance then please guide me
     
  2. jcsd
  3. Sep 14, 2010 #2

    Borek

    User Avatar

    Staff: Mentor

    I guess you use Fortran?

    Try to strip your program of everything that is not necessary and get to the point when you have only code that reproduces your problem.

    In my experience you will either find a bug while removing other parts of the code, or you will end with just a few lines that you will be able to post here.
     
  4. Sep 14, 2010 #3

    FZK

    User Avatar

    yes i am unsing fortran.
    Please tell me in details....
    If you want programme i can post it here...

    Reply me
     
  5. Sep 14, 2010 #4

    Borek

    User Avatar

    Staff: Mentor

    Remove all unnecessary functions, variables, declarations, everything that is not related to the problem - leave just enough code so that it can be compiled and reproduces the problem when is executed.
     
    Last edited by a moderator: Aug 13, 2013
  6. Sep 14, 2010 #5

    FZK

    User Avatar

    Thanks for your nice reply.....here i am sending the exact portion of my problem so please see that and comment on....
    In this programme i decleared bparz(1500) in a named common block statement like
    common/gammaz/bparz(0:1500)...
    Now i want to use these common block array elememts in main function....
    But i cannot able to transfer it
    ***********************************
    do 503 z=0,nz
    do 500 j=0,nsprd
    do 501 nb=0,nephas
    do 502 i=1,npart
    gaz(z)=ga(i,j,nb)
    bperz(z)=bper(i,j,nb)
    bparz(z)=sqrt(1.-(bperz(z)*bperz(z))-1./(gaz(z)*gaz(z)))
    502 continue
    501 continue
    500 continue
    503 continue
    ****************************************
    now tell me what should i do
     
  7. Sep 14, 2010 #6

    Borek

    User Avatar

    Staff: Mentor

    This is not what I asked for. This is not code that can be compiled, this doesn't tell anything about how the common block is declared and accessed. Please read my earlier posts again.
     
    Last edited by a moderator: Aug 13, 2013
  8. Sep 14, 2010 #7

    FZK

    User Avatar

    Thanks for your reply
    actually programme was made by several programmers and i am also modify the same programme for my uses wher i want to use common block for transfering data from one subroutine to another subroutine. So please guide me how i can use it and acces it in another subroutine.....please help me
     
  9. Sep 14, 2010 #8

    Borek

    User Avatar

    Staff: Mentor

    Sorry, without further effort on your side and without further information nobody will be able to help.
     
    Last edited by a moderator: Aug 13, 2013
  10. Sep 14, 2010 #9

    FZK

    User Avatar

    Yes i will try myself...but can you help me how i can decleared common block array and how to acces it??????
     
  11. Sep 14, 2010 #10

    Borek

    User Avatar

    Staff: Mentor

  12. Oct 8, 2010 #11
    i dont knw abt fortran but....in C/C++ i do like this...

    void abc()
    {
    static int arr[8];
    // do ur processing and then call main..
    main(arr);

    }

    int main(int *arr)
    {
    // here you can use that arr variable... but one little thing you need that array to b static remember...
    now you can easily use it..
    }
     
  13. Oct 8, 2010 #12

    Borek

    User Avatar

    Staff: Mentor

    Why static, if anything, it is better to allocate memory dynamically and pass just the pointer. But this is completely OT.
     
  14. Oct 8, 2010 #13
    hmm.....
     
  15. Oct 8, 2010 #14

    Mark44

    Staff: Mentor

    Calling main and passing the array address is pretty unusual, since the main function is the usual entry point for an application in C and C++ .
     
  16. Oct 9, 2010 #15
    Yes, ur rite Mark.... i mean but its completely possible .... i just gave the shortest solution...:P there are a thousand other ways you can do that..:)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to pass array element form a subroutine to main function
Loading...