An IDL program consists of one or more IDL commands, and may or may not be compiled before execution. IDL programs can take any of the following forms:
A batch file contains one or more IDL statements or commands. Each line of the batch file is read and executed before proceeding to the next line. This makes batch files different from main-level programs, which are compiled as a unit before being executed, and named programs, in which all program modules are compiled as an unit before being executed. A file created by the JOURNAL routine is an example of an batch file. For information on running include files, see Executing Batch Jobs in IDL.
| Note |
Main-level programs are entered at the IDL command line, and are useful when you have a few commands you want to run without creating a separate file to contain your commands. Main-level programs are not explicitly named; they consist of a series of statements that are not preceded by a procedure or function heading. They do, however, require an END statement. Since there is no heading, the program cannot be called from other routines and cannot be passed arguments. When IDL encounters a main program as the result of a .RUN executive command, it compiles it into the special program named $MAIN$ and immediately executes it. Afterwards, it can be executed again by using the .GO executive command.
The following example creates a simple main-level program:
A = 2
.RUN at the IDL command line. The command line prompt changes from IDL> to -.A = A * 2 PRINT, A END
.GO at the IDL command line. The main-level program is executed again, and now IDL prints 8.Longer routines and programs, consisting of more than a few lines, are typically given explicit names, allowing them to be called from other programs as well as executed at the IDL command line. Named programs are stored in disk files created using a text editor.
| Note |
Files containing named programs are generally named with the name of the program and the filename extension .pro. See Compiling and Running Your Program for additional information on file naming.
| Note |
Most IDL applications consist of one or more IDL procedures, functions, object definitions, and object method routines:
For example, suppose you have a file called hello_world.pro containing the following code:
PRO hello_world PRINT, 'Hello World' END
This IDL "program" consists of a single user-defined procedure.
IDL program files are assumed to have the extension .pro or the extension .sav. When IDL searches for a user-defined procedure or function, it searches for files consisting of the name of the procedure or function, followed by the .pro or .sav extension.
Procedures and functions can also accept arguments and keywords. Both arguments and keywords allow the program that calls the routine to pass data in the form of IDL variables or expressions to the routine.
For example, the previous user-defined procedure could be changed to include an argument and a keyword:
PRO hello_world, name, INCLUDE_NAME = include
IF (KEYWORD_SET(include) && (N_ELEMENTS(name) NE 0)) THEN BEGIN
PRINT, 'Hello World From '+ name
ENDIF ELSE PRINT, 'Hello World'
END
PRO hello_world, name, INCLUDE_NAME = include
IF (KEYWORD_SET(include)) THEN PRINT, 'Hello World From ' + $
name ELSE PRINT, 'Hello World'
END
Now if the INCLUDE_NAME keyword is set to a value greater than zero, the above procedure will include the string contained within the name variable if a value was supplied for the name argument
Procedures and functions are collectively referred to as routines. An IDL program file may contain one or many routines, which can be a mix of procedures and functions.