Interactive Mode vs. $MAIN$ or Named Programs

The following table compares the features and functionality of the command line with $MAIN$ programs and named programs (procedures and functions). This section compares these methods so that you can quickly decide which mode to use to achieve a particular goal.

Table 7-1: Comparison of Command Line, $MAIN$ and Named Programs

Feature or Functionality
Command Line
$MAIN$ program
Named Programs

Variables—Scope

Uses variables at the main IDL level. Variables created are at the main level (main scope).

No direct access to variables at the main level. Data variables must be passed in or out.

Compilation

None

Manual

Manual or automatic compilation.

Repeatability

Only in current session.

Can run again at any time.

Can run again at any time.

Saved File

Can be saved in a batch file.

Can be created at the command line or in a saved file.

Yes. Requires same name as main routine

Can make calls to other routines

Yes

Yes

Yes

Can be called from other routines

No

No

Yes

Flow Control (Data management)

No

Yes. Can use loops, IF statements, etc.

Modular

No

No

Yes. Routines can be saved separately or together in one file.

For more information and examples of each of these methods, see the following sections in this tutorial:

Scope

As a programmer, you need to know whether variables are within the main IDL scope or the local scope of a routine. The term scope refers to the context in which variables are available to other routines.

In most computer languages, a program runs in its own "scope" or execution context. This isolates each program in the computer's memory. Programs cannot access each others' data variables unless the variables are explicitly passed from one program to another or are managed using shared memory or global variables.

IDL allows greater access to data variables to support interactive use via the command line. Variables stored in IDL's main scope are available to be read or modified by any command, expression, or $MAIN$ program executed at the IDL command line. Main scope variables persist in IDL's memory until they are deleted or the IDL session ends.

Note
We use the term "main scope" to explain the concept that the variables are available to IDL itself. Variables defined in IDL programs are in the "local" scope of the program and are not available to the command line or $MAIN$ programs.

Local Scope

The variables defined in a routine are "local" to that routine and not available outside of the scope of that routine. Let's look at an example to illustrate the concept.

The variable defined here is just for the use of the routine:

PRO ex_simple_oplot 
 
  ; Create a simple dataset: 
  D = SIN(FINDGEN(100)/EXP(FINDGEN(100)/50)) 
   
  ; Create an X-Y plot of vector D: 
  PLOT, D 
   
  ; Overplot the sine of D as a thick, dashed line: 
  OPLOT, SIN(D), LINESTYLE = 5, THICK = 2 
END 

The variable D is defined within the routine and is not available to the command line or to $MAIN$ programs, which are at the main IDL level. In other words, you could define a variable D at the command line, but it would not affect the variable in the routine.

Main Level Scope

Variables in the main level scope are available to the command line and to $MAIN$ programs. This means that you can define or change variables at the command line, and any routines run at the command line or in $MAIN$ programs have access to those variables.

The $MAIN$ Program Examples section shows how a $MAIN$ program uses a variable that can be changed at the command line.