Fortran: Passing loop names to subroutines

In summary: Thanks for the input!In summary, the programmer is looking for a way to conditionally exit all nested subroutines and return to the main program. There are multiple ways to do this, but the easiest is to pass the data from the main program into the nested subroutines inside an object. When the data is processed, the "finished" switch in the object can be changed to TRUE, and the main subroutine can be called again.
  • #1
dervish
4
0
Hello,

A Fortran program I'm working on has a few nested, recursive subroutines, with many entry points between each one (there are lots of places in the first subroutine where calls to the 2nd depth subroutine are made, etc.).

I'm looking for a way to conditionally exit all subroutines and return to the main program (and then cycle some named loop in this program). An ideal way to do this would be to pass the outer loop information from the main program into the subroutines.

Is this possible?

The only other way I can think of doing this would be to conditionally exit each nested subroutine one at a time, however this would add hundreds of lines of code (checking after each entry point to a deeper subroutine whether the exit condition had been reached).

Any ideas would be really appreaciated.
Thanks!
 
Technology news on Phys.org
  • #2
One your main subroutine call from the main program, could you add a switch or something that tells it for example, that the current data point is done being manipulated. At that point, it skips over the nested subroutine calls.

Or perhaps you add another subroutine which when called sends the information back to the original subroutine with information regarding the next data point. i.e. Let's say you have an array, ni x nj x nk big that your looping over. You grab a points data and proceed through your nested recursive subroutines, where your doing something.

Now, instead of just sending that points' data, put it in a data object along with the index information, i,j,k. Then, you can call back to the main subroutine which the resultant information, along with the command to start the next point.

Or...maybe I'm misunderstanding you completely.
 
  • #3
Thanks for the quick reply!

I'm not sure if switches can work like that in Fortran. So far I haven't been able to find any way to exit out of more than one layer of nested subroutines at a time - if this could be done it would be ideal (a conditional exit from all nested subroutines would be sufficient for what I'm trying to do).
 
  • #4
I'm pretty well verse in Fortran. What I'm saying is that not necessarily try to exit out of all the subroutines. That seems too hard; there's no information on where you're at, or how deep you are.

Instead, pass the data from subroutine to subroutine not by itself, but inside an object. The object then will contain all the information regarding not only the information, but "where" you're at. Then, when you're done processing the data, call the original program, or subroutine or whatever. After the data has been processed change a "finished" switch in the object to TRUE. Then, when you call the original subroutine again, it knows to go to either the next point, or continue with the rest of the program.
 
  • #5
Thanks minger, that's probably what i'll have to do. The only problem is that calling the original subroutine will increment the recursion depth again, eventually i'll need to exit from each at least once (involving quite a few checks for the 'finished' switch).

Still, that's not really a problem, it just means a few more lines of code.
 
  • #6
Or you just run until you run out of data, hit and end of file and "bomb" out the program. Not best practice, but it might be the easiest.
 

1. What is Fortran?

Fortran is a general-purpose, compiled programming language that was developed in the 1950s for scientific and engineering applications. It is designed for high-performance computing and is commonly used in fields such as computational fluid dynamics, weather forecasting, and nuclear engineering.

2. How do you pass loop names to subroutines in Fortran?

In Fortran, loop names are called "do labels" and can be passed to subroutines by using the "ENTRY" statement. This statement allows the subroutine to be called by different names, including do labels. The "ENTRY" statement must be included in both the subroutine and the main program in order for the loop names to be passed correctly.

3. Why would you want to pass loop names to subroutines in Fortran?

Passing loop names to subroutines in Fortran allows for more flexible and modular code. It allows the same subroutine to be used for different loops in the main program, reducing the need for duplicate code. It also makes the code more readable and easier to maintain.

4. Can you pass multiple loop names to a subroutine in Fortran?

Yes, you can pass multiple loop names to a subroutine in Fortran by using the "ENTRY" statement for each loop name. The subroutine must have corresponding "ENTRY" statements for each loop name in order for the code to compile correctly.

5. Are there any limitations to passing loop names to subroutines in Fortran?

One limitation of passing loop names to subroutines in Fortran is that the loop names must be defined in the main program, not within a subroutine. This means that the loop names cannot be changed or modified within the subroutine. Additionally, the "ENTRY" statement can only be used for labels within the same program unit, so loop names cannot be passed across different program units.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
16
Views
5K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
18
Views
6K
Back
Top