How to pass array element form a subroutine to main function

In summary: Thanks for the reply. I was looking for something more specific. Thanks.In summary, this programmer tried to transfer an array of data from one subroutine to another, but was unsuccessful. He suggests stripping the code down to the problem, or finding a bug in the code. Fortran is not mentioned.
  • #1
FZK
6
0
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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
borek said:
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.

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

Reply me
 
  • #4
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:
  • #5
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
 
  • #6
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:
  • #7
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
 
  • #8
Sorry, without further effort on your side and without further information nobody will be able to help.
 
Last edited by a moderator:
  • #9
Yes i will try myself...but can you help me how i can decleared common block array and how to acces it?
 
  • #11
i don't 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..
}
 
  • #12
Why static, if anything, it is better to allocate memory dynamically and pass just the pointer. But this is completely OT.
 
  • #13
hmm...
 
  • #14
Msid17 said:
i don't 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..
}

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++ .
 
  • #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..:)
 

1. Can you explain the concept of passing array elements from a subroutine to the main function?

When you pass an array element from a subroutine to the main function, you are essentially transferring the value of that specific array element from the subroutine to the main function. This allows you to use the value of the array element in the main function, without having to declare it again.

2. How do you pass array elements from a subroutine to the main function in programming?

In order to pass array elements from a subroutine to the main function, you need to use the return keyword in the subroutine to send the value of the array element back to the main function. In the main function, you can then use the return value to store and use the array element.

3. Is it possible to pass multiple array elements from a subroutine to the main function?

Yes, it is possible to pass multiple array elements from a subroutine to the main function. This can be done by using the return keyword multiple times in the subroutine, each time sending a different array element's value back to the main function.

4. What are the benefits of passing array elements from a subroutine to the main function?

Passing array elements from a subroutine to the main function can make your code more efficient and organized. It allows you to reuse the value of an array element without having to declare it again in the main function. It also helps in reducing code duplication and makes the code more readable.

5. Are there any limitations to passing array elements from a subroutine to the main function?

One limitation of passing array elements from a subroutine to the main function is that it can only be done with simple data types, such as integers or strings. Complex data types, such as arrays or objects, cannot be directly passed from a subroutine to the main function. However, there are ways to work around this limitation, such as passing a reference to the array or using data structures.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
7
Replies
235
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
744
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
2
Views
8K
Back
Top