5
$\begingroup$

Is it allowed to define local functions inside the Module in Mathematica? For example, out of the two codes below which one is correct?

First:

plt1[a_, b_] := 
    Module[{x, y},
        f[x_, y_] := Sin[x*y];
        Plot3D[f[x, y], {x, -a, a}, {y, -b, b}]
    ];


plt1[1, 2]

Second:

plt2[a_, b_] := 
    Module[{f},
        f[x_, y_] := Sin[x*y];
        Plot3D[f[x, y], {x, -a, a}, {y, -b, b}]
    ];


plt2[1, 2]
$\endgroup$
4
  • 4
    $\begingroup$ The first one makes x,y local, but f global. Making x,y, local is not necessary, because Plot3D localizes its variables. The second one makes f local. Therefore, the second one is the right way. $\endgroup$ Jan 8, 2022 at 14:15
  • $\begingroup$ @Daniel Your point is made very clearly in your comment. Perhaps you could convert it to an answer. $\endgroup$
    – MarcoB
    Jan 8, 2022 at 17:57
  • $\begingroup$ @Daniel Thanks a lot for the clarifications. $\endgroup$
    – Harken
    Jan 8, 2022 at 22:12
  • $\begingroup$ You have to be careful with local functions like your f in the second example, since often (but not in your case above) they will not be automatically garbage-collected. I recommend reading this Q/A for more information on that. $\endgroup$ Jan 10, 2022 at 16:48

2 Answers 2

5
$\begingroup$

The first example makes x,y local, but f global. Making x,y, local is not necessary, because Plot3D localizes its variables. This leaves f global what can creates troubles.

The second example makes f local and does not unnecessarily localize x and y. Therefore, the second example is the right way.

$\endgroup$
4
$\begingroup$

Yes. Have a look at the localized definition of a function using a localized symbol.

Module[{f}, f[x_] := x^2; DownValues[f]]
(* {HoldPattern[f$6838[x_]] :> x^2} *)

Module renames the localized symbols. In this case f becomes f$6838. You may do the same things with a localized symbol that you can do with a global symbol.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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