Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
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
add comment

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.

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
2  
@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
add comment

Your Answer

 
discard

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.