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.

I want to write a Package with a function in it. fun[]:= (DynamicModule[{},(a=10;a)]) . This works fine,but when i call it twice, they are related to each other. how do i localize the values?

share|improve this question

closed as too localized by Ajasja, Artes, Mr.Wizard Jan 19 '13 at 8:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

I recommend reading the help pages in the documentation center of Mathematica on DynamicModule. Let's consider the first example that is shown in the help and assign it to a function fun[]:

fun[]:= DynamicModule[{x}, {Slider[Dynamic[x]], Dynamic[x]}]

If you now call fun[] twice, the variable x will be local in each DynamicModule. The first Slider won't change the value of x in the second Slider. Your achieve a local variable by putting it into the curly brackets at the beginning of your DynamicModule (similarly to Module).

For your example, you have to put a into the curly brackets. Otherwise it will be globally accessible:

fun[]:= DynamicModule[
    {a},
    a=10;
    a
]

Now a is a local variable inside the DynamicModule assigned to fun[].

share|improve this answer

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