There is some pages in the jmp help that explains how scope works in jmp, and how names are resolved:
Local Namespaces
Advanced Scoping and Namespaces
Reference Namespaces and Scopes
By default most variables will be put in the global scope, shared across the jmp session.
Running the script
a = 1;
show(a);
will show a = 1
running
show(a);
a = 3;
show(a);
will show a = 1, then a = 3 as the variable is shared.
Scripts can be (somewhat) isolated by putting Names Default to Here(1) in the script.
This creates a scope "here" that is only accessible by the script, and exist for the lifetime of the script
Names Default to Here(1);
show(isempty(a));
a = 1;
show(isempty(a));
If the above script will show 1, 0 the first time its run and 0, 0 the second time its run, closing the script window resets the here scope.
There is also local (and local here) blocks, where we can specify variables that a only accessible to that part of the code.
Names Default to Here(1);
a = 1;
b = 2;
show(a); // 1
local( {a},
a = 3;
show(a); // 3
show(b); // 2
);
show(a); // 1
Finally there are namespaces, which creates a "place" where we can put variables.
Names Default to Here(1);
ns = New Namespace("test");
a = 1;
ns:a = 3;
show(a, ns:a); // 1, 3
Note that namespaces are globally accessible, so if you have two scripts that both a namespace with the same name, they will access the same variables. This can be mitigated by using anonymous namespaces.
What can trick people, and sounds like the problem you're having is that variables in functions default to Global/Here scope.
Names Default to Here(1);
f1 = Function({},
a = 1;
);
a = 2;
show(a); // 2
f1();
show(a) // 1
There is the option to either specify which variables should be local to the function, as txnelson explained. there is also the {Default Local Option}
Names Default to Here(1);
f1 = Function({},
i = i+1;
);
f2 = Function({},{Default Local},
i = i+1;
);
//shows 2,4
For(i = 1, i <= 5, i++,
f1();
show(i)
);
//shows 1,2,3,4,5
For(i = 1, i <= 5, i++,
f2();
show(i)
)
It sounds like this is the problem you mentioned.
Other peculiarities:
There some peculiarities with functions compared to most other languages. if a variable isn't defined in the local scope JMP will look at the surrounding scope. For functions this is the entire call stack.
Names Default to Here(1);
show_fun = Function({},{Default Local},
show(a,b);
);
f1 = Function({},{Default Local},
a = 3;
show_fun();
);
f2 = Function({},{Default Local},
b = 4;
f1();
);
a = 1;
b = 2;
show_fun(); // 1,2
f1(); // 3,2
f2(); // 3,4