# Defining a user defined function in DynamicModule

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

h[x_] := x^2


I want to find h[2].

-
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

## 1 Answer

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

DynamicModule[{h},
h[x_] := x^2;
h[2]
]


4

If this fails perhaps you could explain in what way.

-
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