Data editing > Using subroutines in the edit > Writing your own routines > Writing subroutines in Quantum > Defining variables in a subroutine
 
Defining variables in a subroutine
All local variables named on the subroutine statement must be defined in that subroutine. Real or integer variables passed to the subroutine must be defined as such in the routine. For example:
subroutine conv(gallons,liters,price)
/* number of gallons bought
int gallons
/* equivalent in liters
real liters
/* price per gallon
real price
Single data variables (columns in the C array or user-defined data variables with one cell only) are passed to a subroutine by naming the variable on a data statement as shown here:
subroutine chk(flav,prefb)
/* flavors bought
data flav
/* brand preferred
data prefb
Fields of data variables are passed as integers with the definition:
subroutine ctyp(car)
/* make of car owned
int car
Any multicodes present in this field are ignored. If you have a multicoded field and you want to be able to access the codes in each multicode, you must treat the field as a series of single data variables and pass each one separately, using a data statement, rather than passing the field as a whole. When variables are passed with call they are written in exactly the same way as you would write them anywhere else in your edit. For example:
call sub1(c15,gallons,cost,c(20,28))
passes the address of the data variable c15, and the integer values of the variables gallons and cost and the field c(20,28).
Here is a chart summarizing how to define variables for subroutines:
Main definition
Call argument
Subroutine argument
Subroutine definition
int item 1
item
purch
int purch
int shop 5s
shop3
shop
int shop
real cost
cost
cost
real cost
data c 1000s
c(10,11)
week
int week
data c 100s
c15
pref
data pref
data tried
tried
tried
data tried
Notice that in the main definitions the size of the variable is defined, whereas in the subroutine definition no size is required since all values are passed as integer values or, in the case of a single data variable, as an address.
See also
Writing subroutines in Quantum