1
votes

I'm customizing a commercial code which deeply uses common block to define global variables.

What I would like to do is to pass only one of those variable to a subroutine, but not making the include, because I don't need the other several variables defined as common.

The only way I found to do this has been to previously define a new local variable, assign to it the value of the global variable, and then pass the new variable to the subroutine, but I don't like that way of proceed..

Is there a solution to tell Fortran to convert a variable to local when passing it to a subroutine?

Here one example:

Main program:

INTEGER :: A
REAL :: Y(20)
COMMON /VARS/ Y, A
INTEGER :: res, transfer_var
transfer_var = A
call sub_test(transfer_var, res)
...

Subroutine:

subroutine sub_test(var1, var2)
INTEGER, intent(in) :: var1
INTEGER, intent(out) :: var2
var2 = 1 + var1
return
end
1
Done, thanks for the advice - Filippo
that example doesn't make any kind of sense to me, is that the original or your 'solution' - steabert
How are you getting A into the subroutine in the case above? I'm looking at the line var2 = A + var1, where is A coming from? Broken record I know, but it really is the case that a COMPLETE, short example showing the problem does really help. - Ian Bush
The subroutine sub_test is clearly buggy. Please, insert IMPLICIT NONE at the top of such subroutine : the compiler will explain you that the variable A has never been defined. - Francois Jacq
If you're stuck with common blocks including the declaration inside the subroutine doesn't actually pass anything, it just gives the subroutine access to the variables in the common block. Personally I think you're tying yourself in knots trying to avoid what the rest of the code doesn't avoid. Hold your nose and insert COMMON /VARS/ Y, A into the subroutine. Or roll up your sleeves and eliminate the common block. But what you are trying to do is neither one nor the other. - High Performance Mark

1 Answers

0
votes

This is a minimal working example of code that does not exhibit the behavior you describe. A is assigned in main, passed by common to sub_one, then used directly as a subroutine argument.

 implicit none
 INTEGER :: A,res
 COMMON /VARS/ A
 A=41
 call sub_one()
 end 

 subroutine sub_one()
 INTEGER :: A,res
 COMMON /VARS/ A
 call sub_test(a,res)
 write(*,*)res
 end

 subroutine sub_test(var1, var2)
 INTEGER, intent(in) :: var1
 INTEGER, intent(out) :: var2
 var2 = 1 + var1
 return
 end

this compiles without issue and returns the result 42.

Can you provide example code that shows the issue?