I am trying to define a function inside a module as following

test1[a_] := Module[{sol1, g1, y},
  sol1 = Reduce[a + x^2 == y, x];
  g1[y_] := Evaluate@sol1[[1, -1]];

This gives the output


-Sqrt[-3 + y$28588]

As you can see this does not work. The y in the body of the function is y$28580, but not y$. Is there any way to make this work?

  • 1
    $\begingroup$ If you use Block instead of Module does that solve the problem? $\endgroup$
    – Jason B.
    Nov 30, 2018 at 19:09
  • $\begingroup$ Use this: SetDelayed @@ {g1[y_], sol1[[1, -1]]} and read this: Enforcing correct variable bindings $\endgroup$
    – Kuba
    Nov 30, 2018 at 21:31
  • $\begingroup$ Let me know if you disagree with marking it as a duplicate. $\endgroup$
    – Kuba
    Nov 30, 2018 at 21:33

1 Answer 1


Just replace the third line by:

g1[Pattern[Evaluate[y], _]] = sol1[[1, -1]];

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