Home Up Next

Subchapters:

Format of Program Files


There are essentially four types of code units in files containing IDL statements (see the summary below): Procedure, function, main program, included file 

1) Procedure

A self-contained code unit with a unique name that is called by other code units to perform a desired function. The calling code unit and the procedure communicate via passed arguments.

Procedure definition

  - Purpose: Specify a procedure name and parameters.
  - Syntax: pro name, parameter_1, parameter_2, ... parameter_n

      name is the name of the procedure.
      It will be saved in a file called name.pro

Examples:

pro test, a, b, c

    if a eq b and b eq c then print,'All equals'

end

-------------------------------------------------------

pro compute, x, y, z, help=help

if keyword_set(help) then begin

    print,'x and y : inputs, z: output'

    return

endif

va_tmp = (x^2 - y^2)

if va_tmp ge 0 then z = sqrt(va_tmp) else z = 0

end

  - Notes: A procedure must end with an end statement and may have one or more return statements inside. If program flow reaches the final end statement a return is implied.

      Example calls to the above procedures:
       

        IDL> test, 2, 3, 2
        IDL> test,x,y,y
       

        IDL> compute, x, y, z, /help
        IDL> compute,2,3,resultat

Rem: The recursivity of procedure and function is allowed.

2) Function

A self-contained code unit similar to a procedure. The only difference is that a function returns a value and can therefore be used in expressions. 

Function definition

  - Purpose: Specify a function name and parameters.
  - Syntax: function name, parameter_1, parameter_2, ... parameter_n

      name is the name of the function. It will be saved in a file called name.pro

Examples:

function test2,a, b, c

    if a eq b and b eq c then return,1 else return,0

end

 

function compute2, x, y, z, flag=flag

    if keyword_set(flag) then print,' In the function compute2'

    va_tmp = x^2 - y^2 - z^2

    if va_tmp ge 0 then return,sqrt(va_tmp) $

        else return,0

end

  - Notes: A function must end with an end statement and must have one or more return

      statements inside. A return statement in a function must include the return value: return,
      value.

      Example calls to the above functions:
       

        IDL> a = test2(2, 3, 5)
       

        IDL> t = compute2(x, y, z, /flag)

 
W A R N I N G !
The parameters are passed to procedures and functions by reference or by value. The variables are passed by reference, and so can be modified into the procedure.
BUT the subscripted variables and tags of structures are passed by value and can NOT be modified by a procedure or a function.
For example, hereafter write will know the value of a(5), but read CANNOT set a value to a(5) or to the structure tag star.mag.

        IDL> print,a(5)
        IDL> readf,lun,a(5) ;
will not work...
        IDL> readf,lun, star.mag ;
will not work...

That's why the best way to read a vector or an array is to define well a variable before (most of the time an array or a structure will be welcome) , and to read all the data in one rush. Or you have to use a temporary variable if you really want to use a loop:

for i=1,N do begin

       readf,lun,temp

       tata(i) = temp

endfor

3) Main Program

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, it cannot be called from other routines and cannot be passed in 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.

4) Included File, batch file

A file to be included in other files. The statements contained in an include file are textually inserted into the including file. A file can contain any combination of functions, procedures, and/or include files. For example, a file might contain three procedures and two functions and also might be included in another file.

 

IDL courses C. Morisset © 2004 IA/UNAM V 2.2

Home Up Next