Variables I defined in Module are not evaluated as I expected

I want to make and use object like stuff of Object-Oriented Programming language(like JavaScript) in Mathematica and am trying it by referring to the answer of this question using Module.

For example, I want to make an object(Module) which has the following properties and Methods:

Properties:

• a : 2 (integer)
• b : 4^a
• f : func[a], with func: function which just returns an argument doing nothing else. (func : x -> x)
• c = 4^f

Methods:

• showA: returns a
• showB: returns b
• showC: returns c

I wrote a code below, but the result was not what I had expected:

In[529]:= func[x_] := (
Return[x]
)

foo = Module[
{
a = 2,
b = 4^a,
f = func[a],
c = 4^f
},
Switch[#,
"showA", a,
"showB", b,
"showC", c
]
&];
foo["showA"]
foo["showB"]
foo["showC"]

Out[531]= 2

Out[532]= 1048576 //expected output: 16

Out[533]= 4^f //expected output :16


What is wrong? If it is impossible to do, what is the alternative simplest way to achieve this?

Any information would be appreciated.

• the bound variables in module can't refer to each other within the first argument; so, Module[{a = 3, b = 4 a}, f[a,b]] would give f[3, 4a], because the a on the rhs of b = 4 a is not the "module version" of a, but the external a. (You can notice this from the syntax highlighting—it's blue, not green.) Commented May 12, 2021 at 5:28
• Also, i'm not sure func[x_] := (Return[x]) means what you might think it means! Return is only used for control flow changes in mathematica, like breaking out of For loops or sequential evaluation (;). You'll still get the expected output from func in this case, but you may as well use func[x_] := x. Commented May 12, 2021 at 5:34
• Thank you. I didn't know {} inside Module refers only to external variables.
– ten
Commented May 12, 2021 at 7:47
• The reason I used Return instead of writing func[x_] := x is just I wanted to know whether it works as a "return" keyword of other ordinary programming languages. The function I need in my actual project requires much more to do than func[x_] := x (including For loop calculation) and I thought I needed to use Return for returning the result.
– ten
Commented May 12, 2021 at 7:58

The code below is the most minimal re-writing of the OP that I could come up with

func[x_] := x
foo = Module[{a = 2},
b = 4^a;
f = func[a];
c = 4^f;
Switch[#, "showA", a, "showB", b, "showC", c] &];
foo["showA"]
foo["showB"]
foo["showC"]


and gives the following as output

2
16
16


Is this what you were looking for?

• Thank you. It worked. I didn't know {} inside Module refers only to external variables.
– ten
Commented May 12, 2021 at 8:11
• But b, f, & c are now global. I would declare them in the module header, but assign values in the body. Commented May 12, 2021 at 8:11
• sorry, maybe I misunderstood what you meant earlier. Just to be clear, do you need all variables to be defined in the Module?
– user49048
Commented May 12, 2021 at 8:20
• Yes, it is preferable to trap variables in the Module for not coming upto global scope.
– ten
Commented May 15, 2021 at 0:40