How to use Evaluate inside function definition so that is acts as Evaluate only whet function is called?

This is toy exampl, where I want to define function inside another function:

Clear[y, x];
f1[a_] := Module[
   {test, testF},
   test = a x + 2;
   testF[x_] := Evaluate[test];

I want result "2 + 6 y", but get "2 + 6 x"

  • 2
    $\begingroup$ Evaluate is completely unnecessary here -- you could just as well define test[x_] := a x + 2. I assume your actual problem is more complicated than this, but your minimal example is not a very good one. Perhaps you should include your actual question, even if more complicated. See also What is the XY problem?. $\endgroup$ Dec 6 '18 at 2:38
  • 2
    $\begingroup$ To be more specific, Clear[y, x]; f1[a_] := Module[{test, testF}, test = a x + 2; Set @@{testF[x_], test}; testF[y]]; f1[6] will resolve your problem. Notice Evaluate is not relevant here, what's causing trouble is renaming inside Module, which has been explained in the post linked above. $\endgroup$
    – xzczd
    Dec 6 '18 at 6:24

Browse other questions tagged or ask your own question.