Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I define a user defined function within DynamicModule? For example, if

h[x_] := x^2

I want to find h[2].

share|improve this question
This question is not clear to me; can you give an extended example, please? – Mr.Wizard Oct 9 '12 at 20:18
@Mr.Wizard: Some what like this. DynamicModule[{h[x_] := x^2}, h[2]] so that it can be deployed intp CDF. – Jennifer Oct 9 '12 at 20:35

I don't have experience deploying to CDF (version 7), but I don't see anything to stop you from doing:

 h[x_] := x^2;


If this fails perhaps you could explain in what way.

share|improve this answer
It works but if there are more than 1 user defined functions – Jennifer Oct 9 '12 at 20:55
@Jennifer then add them just like the first: DynamicModule[{h, i, j} ... `h[x_] := x^2; i[x_] := Sqrt @ x; j[x_] := Sin[x^4]; -- perhaps I still don't understand? – Mr.Wizard Oct 9 '12 at 20:59
It shows error for this DynamicModule[{h, i, j}, h[x_] := x^2; i[x_] := Sqrt@x; j[x_] := Sin[x^4]; h[2], i[3], j[5]] – Jennifer Oct 9 '12 at 21:03
@Jennifer you need to watch your syntax more closely; you accidentally are passing additional arguments to DynamicModule. Try this instead: DynamicModule[{h, i, j}, h[x_] := x^2; i[x_] := Sqrt@x; j[x_] := Sin[x^4]; {h[2], i[3], j[5]} ] – Mr.Wizard Oct 9 '12 at 21:04
Oh yes. Thanks. Now working – Jennifer Oct 9 '12 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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