# Why are the inputs not being applied to the function in this MWE?

Consider the following MWE

Clear["Global*"]

randFunc[listFunc_] := RandomChoice[listFunc]@x;
mwe[list_] := Module[{testfun1, testfun2},
testfun1[x_] = RandomChoice[list]@x;
testfun2[x_] = randFunc[list];

N@{testfun1[2], testfun2[2]}
];

mwe[{Sqrt, Log}]


Which gives output that looks like {0.693147, Log[x]}

The numbers/function changes because of RandomChoice, but why does testfun1[2] evaluate but not testfun2[2]?

Note: The following code (where the list is explicitly input in testfun1 and testfun2 does work

Clear["Global*"]

randFunc[listFunc_] := RandomChoice[listFunc]@x;
testfun1[x_, list_] = RandomChoice[list]@x;
testfun2[x_, list_] = randFunc[list];
N@{testfun1[2, {Sqrt, Log}], testfun2[2, {Sqrt, Log}]}


Which gives output like {1.41421, 0.693147} (again, RandomChoice can change values, but they are both evaluating)

Note 2: I can get the expected behavior in the first example by changing testfun2[x_] = randFunc[list] ;to testfun2[y_] = randFunc[list] /. x -> y;, but I don't understand why I would need to do this.

Looking at the trace, Maybe randFunc[list] is not evaluating when testfun2[x_] is being defined in the Module, but I don't understand why this would be?

• (maybe the HoldAll attribute of Module?)

Consider the following:

Clear["Global*"]
randFunc[listFunc_] := RandomChoice[listFunc]@x;
mwe[list_] :=
Module[{testfun1, testfun2}, testfun1[x_] = RandomChoice[list]@x;
testfun2[x_] = randFunc[list];
Print[Information[testfun2]];
N@{testfun1[2], testfun2[2]}];


where I put "Print[Information[testfun2]]" to actually see the definition of testfun2. If we now say:

mwe[{Sqrt, Log}]


You see that the argument of testfun2 is localized, that mean its name is not "x" but "xDollar". Therefore, the x in Sqrt[x] is not replaced by "xDollar". Toward this aim, you must change your definition:

Clear["Global*"]
randFunc[listFunc_] := RandomChoice[listFunc];
mwe[list_] :=
Module[{testfun1, testfun2}, testfun1[x_] = RandomChoice[list]@x;
testfun2[x_] = randFunc[list][x];
Print[Information[testfun2]];
N@{testfun1[2], testfun2[2]}];

mwe[{Sqrt, Log}]


• Thank you. This answer is exactly what I was looking for. I had a feeling it had to do with something like this (and I noticed the x\$_ when using Trace) but I couldn't put everything together how I wanted. May 29 '21 at 18:35