Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Info to Subroutines in Fort 90/95

Status
Not open for further replies.

Stevridge

Programmer
Sep 8, 2010
7
CA
Two questions:
1) In the main program data is read in plus claculations using many allocatable arrays. One of the arguments in subroutine A called is another subroutine B. B needs much of the info from the main program that A doesn't need. How can I get the info to B without passing it through A (which might also call C which doesn't need all the information B does)? Because of the allocatable arrays, I don't see how a module or common can be used.

2)I would prefer to put the reading in and most of the calculations into a subroutine. Would this change the answer to (1)?
 
The easiest way is to use a module grouping together all the variables the subroutine B needs

Optionally, the routine B could be put in that module too ! Idem for the routine D in charge of allocating and initializing these arrays.

Here is the version with the subroutines C and D in the module (the version I advise because I like to see all the subroutines and functions within a module).

If you want to keep the routine B outside the module, then you just have to insert a "USE data_mod" at the top of that routine (and even better at top of the other module containing the routine B).

Code:
MODULE data_mod
  IMPLICIT NONE
  INTEGER, ALLOCATABLE :: index(:)
  CONTAINS
  SUBROUTINE b
    write(*,*) index
  END SUBROUTINE
  SUBROUTINE d(n)
    INTEGER,INTENT(in) :: n
    ALLOCATE(index(n))
    WRITE(*,'(A,I0,A)',advance='no') 'Enter ',n,' values : '
    READ(*,*) index
  END SUBROUTINE
END MODULE

PROGRAM main
  USE data_mod
  CALL d(2)
  CALL a(b)
END PROGRAM

SUBROUTINE a(sub)
  EXTERNAL :: sub
  CALL sub()
END SUBROUTINE

 
Thanks for your response. I'll set it up the way you recommend but I think there might be some problems. I'll wait until I've finished it all so I can be more specific if there are issues.
 
xwb, I'm trying to avoid optional arguments. The problem is that sub A calls subs B, C, D, E, ... and while there are overlaps, in general the subs require different information to be passed to them by A. I.m finding it too messy to employ optional arguments. There are also some other considerations that hinder that approach. Thanks for your response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top