I have the following Function:

Testing[g[x_], y_] := Module[{s = x, t = y, c }, c = s + t ; c] 

Which I am just trying to get to work before doing my actual application. It should accept a function g[x] and do stuff with it.

As a test, I try:

f[x_] := x^2;
Testing[f[x], 4]

But it just returns

Testing[x^2, 4]

which means that it does not understand what I mean...? What is the correct syntax to feed a mathematical function into my Function?

  • $\begingroup$ Testing will only work for something with head g while f[x] evaluates to Power[x,2]. $\endgroup$ – Kuba Nov 23 '16 at 16:02
  • $\begingroup$ Why not just run Testing[x_, y_]? $\endgroup$ – Feyre Nov 23 '16 at 16:09
  • $\begingroup$ If you want it to accept a function in the form g[x], but with any kind of name (instead of g specifically) and any kind of variable, then you should use Testing[g_[x_], y_] := .... Moreover, you should probably make Testing HoldFirst to keep the function from evaluating too early: SetAttributes[Testing, HoldFirst] $\endgroup$ – Sjoerd Smit Nov 23 '16 at 16:34

As I understand you, what you want is that c becomes x^2 + 4. This gives you that polynomial with an anonymous variable instead of x:

Testing[g_, y_] := Module[{s = g[#], t = y, c}, c = Evaluate[s + t] &; c]
f[x_] := x^2
Testing[f, 4]

4 + #1^2 &


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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