Types of IDL Programs

There are multiple ways of writing and executing programs within IDL. These involve varying levels of complexity and include $MAIN$ programs, procedures, and functions.

$Main$

You typically create a $MAIN$ program at the IDL command line when you have a few commands you want to run without creating a separate file. $MAIN$ programs are not explicitly named by a procedure (PRO) or function (FUNCTION) heading. They do require an END statement, just as procedures and functions do. Since they are not named, $MAIN$ programs cannot be called from other routines and cannot be passed arguments. $MAIN$ programs can be run from the command line using the .RUN command or saved in a file and run later.

When IDL encounters a main program either as the result of a .RUN command or in a text file, it compiles the code into the special program named $MAIN$ and immediately executes it. Afterwards, it can be executed again using the .GO command.

Note
Only one main program unit may exist within an IDL project window at any time.

Named Programs: Procedures and Functions

Procedures and functions are both modular programs that can be run individually and called from other programs. A program may include multiple procedures and functions and call as many other programs as necessary. Developers can choose whether to save many individual procedures and functions or to combine them in the same file. Reusing the same procedure or function for multiple programs can be a deciding factor in saving them separately. See the following sections for more information about how procedures and functions differ and when to use them.

Note
To view programs written in IDL, use the demo programs that come with IDL, found in the \examples\demo\demosrc of the IDL distribution. Open and view these programs to help you understand procedures and functions, but don't save any changes you make, as you and other users may use these programs for demonstration and training purposes.

Procedure

A procedure is a self-contained sequence of IDL statements that performs a well-defined task. A procedure is identified by a procedure definition statement (PRO <procedure_name>), where the procedure name is the name of the IDL statement you are creating. Parameters are named variables that are used in the procedure.

Use procedures when you are working on data "in place" or when no value is returned from the program. For example, a procedure could create a plot display on the screen but return no values back to IDL.

Function

A function is a self-contained sequence of IDL statements that performs a well-defined task and returns a value to the calling program unit when it is executed. All functions return a function value which is given as a parameter in the RETURN statement used to exit the function. A function is identified by a function definition statement (FUNCTION <function_name>), where the function name is the name of the IDL statement you are creating.

Use functions when you need easy access to a returned value, since functions create a new variable by default.

See Overview of IDL Program Types for more information.