Usually if I want to define a function using an expression I do:


which usually works and give f a defination.

But now if I want to do the same inside a Module:

f = Module[{g, x,expr},
    expr = x^2;
    g[x_] = expr

then Information[f] would show that f has not a defination but instead an assignment. Why the first method does not work inside a Module?

  • $\begingroup$ I suggest that you define functions the way the documentation recommends. $\endgroup$
    – Ghoster
    Apr 22, 2023 at 0:59
  • $\begingroup$ @Ghoster What should I do if I have an expression in terms of a symbol and I want to make it into a function of that symbol? $\endgroup$
    – Cheng Tao
    Apr 23, 2023 at 16:46

1 Answer 1


Try evaluating just

  {g, x, expr}, 
  expr = x^2;
  g[x_] = expr]

You'll notice that this actually evaluates to something (it'll be something like x$41742^2). Furthermore, the symbol g inside the module is localized to that module (i.e. it's real name is something like g$12345). So, inside the module you've defined DownValues for g$12345, and in doing so, you made the whole Module evaluate to x$41742^2. This you then set to the Global f.

I know you didn't ask, but defining functions this way is fraught. The "this way" I'm referring to is by first creating an expression and then referring to that expression when creating DownValues. If you're going to do it this way, you might want to use Formal symbols (e.g. \[FormalA]). Or there are other ways to protect your definition from collisions.


Your Answer

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

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