6
$\begingroup$

This question already has an answer here:

I have a question. I would like to know what an empty list ({}) means when used in Module[] as the code below shows? Why has a variable not been put inside {}?

Test[x_] := 
  Module[{},
    If[NumberQ[N[x]] && Not[ Head[N[x]] === Complex],
       True,(* then *)  
       False(* else *)   
    ]
  ]
$\endgroup$

marked as duplicate by Leonid Shifrin, m_goldberg, MarcoB, RunnyKine, user9660 Apr 22 '16 at 18:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ It means there are no local variables defined. $\endgroup$ – RunnyKine Apr 22 '16 at 3:10
  • 5
    $\begingroup$ Duplicate $\endgroup$ – andre314 Apr 22 '16 at 8:55
15
$\begingroup$

Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (Module, With or Block). For example:

myFunction[x_]:= 2 Sin[x] + Exp[-x^2]

But as the function definition gets more complex, instead of polluting the Global context with temporary variables, we use one of the scoping constructs mentioned above.

myModuleFunction[x_Integer]:= 
  Module[{pts, cvx},
    pts = RandomReal[4, {x, 3}];
    cvx = ConvexHullMesh @ pts;
    HighlightMesh[cvx, {Style[1, Black], Style[2, Yellow]}]
  ]

But sometimes a non-customary way is to use CompoundExpressions like the following shows:

myCompoundFunction[x_Integer]:= 
  (pts = RandomReal[4, {x, 3}];
   cvx = ConvexHullMesh @ pts;
   HighlightMesh[cvx, {Style[1, Black], Style[2, Yellow]}]
  )

Of course, in this scenario, both pts and cvx are now Global variables, so if you need to use them for further calculations outside the function's scope, they're available to you whereas, in the Module case above they aren't because we made them local. You can make those variables global using Module and this is where we answer your question:

myModuleFunctionGlobal[x_Integer]:= 
  Module[{},
    pts = RandomReal[4, {x, 3}];
    cvx = ConvexHullMesh @ pts;
    HighlightMesh[cvx, {Style[1, Black], Style[2, Yellow]}]
  ]

By not putting symbols defined in the body of Module into the module list, the symbols become global variables. Your function as defined doesn't really need a Module as it's just a simple If statement.

$\endgroup$
  • $\begingroup$ Might consider rewording "By using {}, any new variable defined therein becomes a global variable", it could be read as the empty list argument to Module is somehow needed, where I think something like "By not putting symbols defined in the body into the module list, they will be Global, perhaps unintentionally" is a bit more clear. +1, o/c... $\endgroup$ – ciao Apr 22 '16 at 5:10
  • $\begingroup$ @ciao. Good point, thanks. I've re-worded it as you advised. $\endgroup$ – RunnyKine Apr 22 '16 at 5:19
  • $\begingroup$ Is there any difference in using the global module function approach over the compound function, or just personal preference? $\endgroup$ – shrx Apr 22 '16 at 7:09
  • $\begingroup$ In general, I used the myModuleFunction[] construction. BTW, what's the difference between myCompoundFunction[] and myModuleFunctionGlobal[]? I think they are not good habit in Mathemaica programming. $\endgroup$ – xyz Apr 22 '16 at 7:10
  • $\begingroup$ @shrx I can't think of any difference between those two approaches, except maybe performance-wise, myCompoundFunction might be slightly faster. $\endgroup$ – RunnyKine Apr 22 '16 at 7:59
0
$\begingroup$

Local variables will be put in the first {} in Module[], for example:

f[x_] := Module[{}, t = x^2];
f[2]      (*--> 4*)
t         (*--> 4*)

You will obtain t=4 after you evaluate the codes above, and if you

f[x_] := Module[{t}, t = x^2];
f[2]      (*--> 4*)
t

you will obtain t itself. Namely, t is just a global symbol(don't forget clear value of t before you evaluate)

which means the value of variables in the first {} of Module[] will be clear outside the Module[] if the variables were assigned in Module[].

For you codes, there is no variable assigned so that there is nothing in the {}.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.