Name:Where did my variables go?
Category:IDL
Author:Conor
Tip:

When I first started using IDL I had a big problem - I would load some variables, run a program, and everytime the program crashed, the variables I loaded before would disappear. After much fist shaking, I discovered the problem had to do with variable scope.

When you first launch IDL, you are placed in the Main variable scope. Any variables you declare are part of this scope and can be seen as long as you are in this scope. When you run a function or a procedure, that function/procedure runs in it's own independent scope, with an entirely different set of variables. This is a pretty standard feature in most programming lanuages, because it allows functions and procedures to declare variables with the same names and not clash with eachother.

In IDL when a function or procedure encounters and error and exits you are not immediatly returned to the Main level. Instead the IDL prompt remains in the scope of the procedure that crashed. This has the distinct advantage that you can examine the variables in your routine and see what caused your program to crash in the first place. The problem is that since you aren't in the Main level, you can't access any variables declared in the main level (i.e. your variables disappear). Fortunately this can be easily fixed - IDL has a command 'retall' that returns you to the main level no matter what scope you are in. Be careful when you do this of course - once you retall you will no longer have access to the state of the variables inside your crashed function.

The moral of the story is that anytime a function crashes you should examine the variables (if you need to) to figure out the problem, and then immediately type 'retall' to return to the main level (you will also automatically be returned to the main level if you compile the function while still inside its scope). It's important to return to the main level once you've finished debugging. Otherwise, any additional variables you declare will be lost when you finally do return to the Main level.