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:
common1`x = 1;
common2`x = 2;
f[x_] := Module[{x1 = common1`x, x2 = common2`x},
{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 Context
s 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
common1`y
etc.