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.
We will illustrate the construction and use of procedures with a number
of examples.
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.
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 |
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 |
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 |
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.