IDL has two different types of functions that can be created, functions and procedures. The primary difference is that functions return a value and procedures don't. Also, there is a difference in the syntax of how you call functions and procedures and how you declare them. Here is an example of a function and a procedure:
function test_function
To run functions and procedures in IDL, save them as a .pro file in your IDL path, and make sure that the name of the file is the same as the name of the function or procedure. So for the above examples the first should be saved as test_function.pro, and the second as test_procedure.pro. This naming convention will allow you to call a function or procedure from the command line without having to compile it first. So once you have saved the above files, you can run them from the command line by simply typing:
return,'hello!'
end
pro test_procedure
print,'hello!'
end
print,test_function()
Both functions and procedures take positional and keyword parameters. Positional keywords for a function or procedure are declared as a comma separated list after the function name. Then when a user calls a function from the command line the variables they pass will be included in the function and renamed according to the function declaration. So for instance if you created a procedure:
; prints hello!
test_procedure
; prints hello!
pro test_procedure,variable1,variable2
and then called it from the command line, the result would be exactly what you expect it to be:
print,variable1 + variable2
end
test_procedure,1,5
One important point that needs to be made here is that if a user calls a function without a positional keyword, that keyword will be undefined in the function. So if, given our previous test_procedure, a user entered the following in the command line he would get an error back:
; prints 6
test_procedure,1
This is clearly an undesired behavior. In general this problem is surmounted using the
% Variable is undefined: VARIABLE2.
% Execution halted at: TEST_PROCEDURE 2 /astro/homes/cmancone/idl/test_procedure.pro
% $MAIN$
n_elements() function. n_elements will return 0 if a variable is undefined. It can be used to check and see that every variable exists before your function actually does anything. Typically you can return an error message if a problem is discovered, or provide a default value. Here's an example of both methods:
pro test_procedure,variable1,variable2
For keyword parameters you specify a name for the keyword and the variable it should be assigned to in the function. It is then called using the keyword name. Here's an example:
if n_elements(variable1) eq 0 then variable1 = 0
if n_elements(variable2) eq 0 then variable2 = 0
print,variable1 + variable2
end
pro test_procedure,variable1,variable2
if n_elements(variable1) eq 0 or n_elements(variable2) eq 0 then begin
print,'variable1 and variable2 are required!'
return
endif
print,variable1 + variable2
end
pro test_procedure,variable1,variable2,extra=variable3
if n_elements(variable1) eq 0 then variable1 = 0
if n_elements(variable2) eq 0 then variable2 = 0
value = variable1 + variable2
if n_elements(variable3) eq 1 then value += variable3
print,value
end
test_procedure,1,5,extra=7
; prints 13