When using a long script (that is used to call other scripts and functions), is it possible to find where a variable was set. By looking at the variable in the workplace I would like to be able to find the most recent place that it was set.
Not very easily. Depending on how nasty and convoluted the code is, you can easily have something that is essentially impossible to determine where any variable is/was last set, because your code might use any number of things like recursive calls, global variables, even variable names that were created using eval or evalin. Of course, the use of eval can really screw things up, because you will then not even find code where you ever see the variable name on the left hand side of an assignment. If you want me to be really creative, I can programmatically create a variable name at random. So at some arbitrary, random point, the variable might be created, as if a monkey were typing variable names at random. Eventually, the variable X might be created, or possibly not, since the programmatic monkey is doing uncontrollable stuff.
Do you want me to write a piece of code that has only about 4 lines, yet you will not be able to identify where a variable was last defined, thus on which specific line of code?
Thus, consider code that has a simple branch in it. I might initially define the variable X to be 0. But inside an if statement, X will be reset to have the value pi.
X = 0;
X = pi;
So it might seem the answer is easy. The variable was last defined inside the branch. But what if the branch test is never satisfied? Or what if that test is based on a random number? Or what if the test is based on the result provided by user interaction from an input statement?
My point is, I can create a very simple circumstance where it is literally impossible to decide where the variable was last set, and therefore, what would be the current value of the variable. Does X have the value of 0, or is it pi? Like Schrödinger's cat, you cannot determine whether X has either given value, merely by looking at the code from the outside. Without evaluating the code itself, essentially evaluating every line of code one at a time, you can never determine where that variable was set. And if there is anything of a truly random nature involved (e.g., fisson of an arbitrary U235 atom), then you can provably not determine what happened merely by looking at the code.