IDL Exercise 6

The purpose of this exercise is to learn how to write procedures in the IDL language and see how information is passed between program modules. See the documentation on procedures for further information.

Functions and procedures are IDL program modules. Functions are normally used to compute and return a value. The returned value may be of a number, string, array or structure. A procedure is normally used to carry out an action, such as printing or plotting. However, a procedure may also be made to return a value and a function can be made to carry out an action. Functions and procedures can therefore be used interchangeably by the skilled programmer. As you develop a skill and style you will find that you use functions in some situations and procedures in others. For now, the goal is to understand both procedures and functions so you have the ability to use both.

A procedure is a program written as a text file. The procedure mypro would have the form

PRO mypro, a, b, KW1=u, KW2=v

statements

END

The statements are written by the programmer to carry out the desired actions.

The procedure statement PRO mypro, a, b, KW1=u, KW2=v contains four parameters. Two of them are positional parameters and two are keyword parameters. When you call this procedure from another program you might say something like:

s=10
t=15
m=11
n=12
mypro,s,t,KW1=m,KW2=n

The keyword parameters are identified by the keyword names, but the positional parameters are identified by their location. The following statements would do exactly the same thing.

The compiler identifies the parameters that are identified with keywords and then it takes the remaining parameters in order.

We will illustrate the construction and use of procedures with a number of examples.


  1. Let us construct a real procedure that we will call mypro1. The first example will use only positional parameters. Copy the following statements in your edit window and then save them to a file named mypro1.pro
  2. PRO mypro1,a,b
    b=2*a+3
    END

    The value of b is computed from a. The value of the parameter in the first position will be used in the calculation and then the result will be available via the parameter in the second position.

    Test the program with the following statements. You may enter them from the command line or from a script file.

    s=10
    t=15
    mypro1,s,t
    print,'s=',s,'  t=',t

    s=      10  t=      23
    It is clear that the value of t, the second parameter,  has been changed.  What would be the result of using the statement mypro1,t,s?

    In the example we used the first parameter to bring a value in to the procedure and the second to bring the value out. The information was passed even though the the names in the procedure construction were not the same as the names that were used when it was called. The method used in transferring the information is called passing by reference.

  3. It is possible to have the procedure compute and return other values. One simply adds them to the parameter list. Create the procedure below and save it in a file with the name mypro2.pro
  4. PRO mypro2,a,b,c,d
    b=2*a+3
    c=a^2+2*a+1
    d=b-2*c
    END

    It is clearly intended that a is an input parameter and b,c,d are output parameters for this procedure. Write a script file to test the program. Explain the results of each of the tests shown below. An efficient method is to edit and save the script file with each new variation.  NOTE: If your browser window is narrow the statements will wrap. Type each complete statement on one line in your script.
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro2,s,t,u,v 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro2,u,v,s,t 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro2,v,u 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v

  5. In the previous step we find that the position of a parameter is important. In this step we will use keyword parameters and find that their position is unimportant. Create the procedure below and save it in a file with the name mypro3.pro
  6. PRO mypro3,APARM=a,BPARM=b,CPARM=c,DPARM=d
    b=2*a+3
    c=a^2+2*a+1
    d=b-2*c
    END

    Write a script file to test the program. Explain the results of each of the tests shown below. You should see that the order of the parameters is not important. The results depend only upon which keyword is used. The answers should be the same as in the first and third result in the last step.
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro3,DPARM=v,APARM=s,CPARM=u,BPARM=t 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro3,BPARM=u,APARM=v 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
     
    Keyword parameters can be used to send information both ways, into a procedure and back. In the above example, information goes to the procedure through APARM and comes back through the others. You have to use the keywords exactly as they were written in the procedure, except that they are not case sensitive. The variables you associate with the keywords can have any valid name. Parameters are passed by reference or passed by value, depending on whether the item is a variable or not.
     
     

  7. Here we will use a mixture of position and keyword parameters. You should find that the keyword items are passed independent of position, but that the positional parameters depend on location in the calling sequence. Create a file with the following program.
  8. PRO mypro4,a,BPARM=b,CPARM=c,DPARM=d
    b=2*a+3
    c=a^2+2*a+1
    d=b-2*c
    END

    Test it with the following scripts. Explain the results.
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro4,DPARM=v,s,CPARM=u,BPARM=t 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
    s=10 
    t=15 
    u=4 
    v=-8 
    mypro4,BPARM=u,v 
    print,'s=',s,'  t=',t 
    print,'u= ',u,'  v=',v
     

  9. In this step you will write your own procedure and test it. Create a procedure AVG that accepts a vector x as an input and returns the smallest value, largest value, average value and number of elements. The average value should be returned through a positional parameter and the others should be returned through keyword parameters named BIG, SMALL, NUM.
  10. Useful IDL functions are MIN, MAX, TOTAL, N_ELEMENTS. Look them up in IDL online help to see how they are used.

    After you have made your procedure, construct a test script and verify that it works under as many different conditions as you can. When you are satisfied that your program works as required, submit it to the instructor for evaluation.