# What is the equivalent of FORTRAN 77 COMMON BLOCK in Mathematica?

I am in the process of converting a FORTRAN 77 program to Wolfram Language. I have several COMMON BLOCK statements in FORTRAN 77. My question is how to deal with them in Wolfram Language?

My original approach is to declare all the variables on each COMMON BLOCK as global and them access them as needed on each subroutine. Does this is reasonable.

• This is probably not an answer you are looking for, but I strongly suggest to look at "Big Ball of Mud": laputan.org/mud . I used the advice there fairly successfully to re-factor and convert a large F77 air-pollution simulation code base to C++ and Mathematica. – Anton Antonov Oct 5 '15 at 17:06

If you want to have more than one "common block," one possibility would be to use a named Context to emulate each block. For example, here I define two "blocks," common1 and common2, and define a variable x in each of them. The Module below then uses these values by prefacing the desired x with the desired Context name:

common1x = 1;

common2x = 2;

f[x_] := Module[{x1 = common1x, x2 = common2x},
{x, x1, x2}]

f[0]

(* ==> {0, 1, 2} *)


This shows that the symbol x has three different values inside the Module, and two of them are taken from the "globally" defined common blocks. Actually, normal "global" variables are in the Global context by default, and the use of named Contexts means that associated variables remain undefined in the Global context. So you also get

f[x]

(* ==> {x, 1, 2} *)


where the absence of a global value for x is apparent.

In each context, you can of course define multiple variables such as

common1y


etc.

• worth a mention with this construct if you change a local variable ( say x1 ) in the module you need to assign it back at the end (common1x = x1  ). You could of course just use the verbose common1x` in the code. – george2079 Oct 5 '15 at 19:01