• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Avoiding Global Variables

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Frodo Baggins

New Member
Joined
Aug 29, 2001
Location
City of Dreaming Spires
I'm programming this in Fortran 90.

The overall program follows this format:

Code:
PROGRAM TEST

Declare variables

X = 2

CALL WALMART(FUNC, ...other stuff...)

END TEST

SUBROUTINE FUNC(MONEY, Y)
MONEY = X*Y
END

The problem is simple. I ask for some input (X), then call a routine called WALMART. The routine makes use of a subroutine (FUNC). The problem arises because the output (MONEY) of FUNC makes use of the input of my main program. However, I cannot alter the input of WALMART. Without using global (COMMON) variables, how do I 'pass' X into the subroutine?

To be more specific, WALMART is a canned routine (NAG). It's inputs and outputs are fixed, so I can't simply pass X into WALMART, then pass it again into FUNC!

Help!
 
Last edited:
[warning]Never programmed in Fortran 90[/warning]

1. You can ask for x in the subroutine func.
2. Can you pass it like this "CALL WALMART(FUNC(MONEY,Y,X), ...OTHER STUFF...)?
3. You could save x to a file then read the file in your subroutine.
 
1. You can ask for x in the subroutine func.

Yes. I can do this. Technically, X is an array defined beforehand. I can define the array (again) within the subroutine. But that means if I want different values when running the program, I need to go to my code and change each instance.

2. Can you pass it like this "CALL WALMART(FUNC(MONEY,Y,X), ...OTHER STUFF...)?

I tried. No, it doesn't work.

3. You could save x to a file then read the file in your subroutine.

:(

There must be a way to do this easily.

Edit: I realized that there's an 'easy' (but still silly) way to do this. Just produce a new subroutine to return the value of X. So...

Code:
PROGRAM TEST

Declare variables

CALL WALMART(FUNC, ...other stuff...)

END TEST

FUNCTION GIVEX(dummy)
GIVEX = some value for X
END GIVEX

SUBROUTINE FUNC(MONEY, Y)
X = GIVEX(dummy)
MONEY = X*Y
END FUNC
 
Last edited:
Never programmed Fortran before... but I'll ask anyway...

Could you pass your x variable by reference into your function? Passing by reference would just point to the same object in memory (or the reference to that specific location in memory that your variable lives), rather than duplicating your array.
 
Never programmed Fortran before... but I'll ask anyway...

Could you pass your x variable by reference into your function? Passing by reference would just point to the same object in memory (or the reference to that specific location in memory that your variable lives), rather than duplicating your array.

Unless I'm mistaken, you're always passing by reference...

In any case, there's still no way (I can see) around the fact that you can't pass anything from the fact that you can't modify your call to the WALMART routine. (see #2 from dave's comment above and my reply).
 
Back