Skip to main content

5.6) Using Keywords In Functions


In the last example we passed multiple parameters (temperature and pressure) into an IDL function. Sometimes it is useful to specify optional things to pass in, which may be useful, but are not necessarily required. We do this using keywords.

For example, we can rewrite our function temp_to_theta to use a keyword to control what units are used for the input. Enter the following code into a file called temp_to_theta2.pro:

Function temp_to_theta2, t, p, degC=degC
; Function to convert temperature (K) and pressure (hPa) to potential
; temperature (theta, K) using a reference pressure of ~1000hPa
; If keyword degC is set it then converts input/output from/to degrees C
RoverCp=0.286
p0=1000.
IF keyword_set(degC) THEN t=t+273.15
theta=t*(p0/p)^RoverCp
IF keyword_Set(degC) THEN theta=theta-273.15
return,theta
END