# How to define a function inside a module through Evaluate [duplicate]

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]];
Print[Definition[g1]];
g1[33]
]


This gives the output

Attributes[g1$28580]={Temporary} g1$$28580[y$$_]:=-Sqrt[-3+y$$28580] -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?

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

## 1 Answer

Just replace the third line by:

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