If you have a routine that uses or returns a lot of data, best practice
is to pass
clusters to the routine. Clusters are passed by
reference, which means anything about a cluster passed into a private or
scoped routine can be changed by the routine. This includes adding new
rows to a cluster array or changing the values of cluster fields.
Clusters can be passed into Private or Scoped routines using either the
cluster's name or the cluster's root name. In addition, clusters outside
of the routine can be directly referenced from inside of the routine;
they are globally available.
The routine is called by providing the routine name, the root cluster
name, followed by the name of the cluster being passed into the routine.
From inside of the routine, you can both read and write clusters. This
provides an easy method to pass a lot of variables into a routine without
having to pass in all the individual variable names.
Example 1 – Calculate Tax
Below is a routine that calculates tax. We pass in a cluster containing
the amount and tax rate, and the routine returns the tax due.
cluster taxes: name$, profit, tax_rate
taxes->name$ = 'Sally Sue'
taxes->profit = 1234
taxes->tax_rate = 6.5/100
calculate_tax_due with taxes, returning due
print 'Tax due:'; due; 'for '; taxes->name$
private routine calculate_tax_due with taxes, returning due
due = taxes->profit * taxes->tax_rate
end routine
Example 2 – Meals
Here we define a root cluster and two related clusters. We pass them
into a routine that prints the meal details.
// define root cluster
cluster meal: type$, protein$, liquid$, carb$
// define related clusters
cluster breakfast using meal
cluster lunch using meal
// add data
breakfast->type$ = "Breakfast"
breakfast->protein$ = "eggs"
breakfast->liquid$ = "tea"
breakfast->carb$ = "toast"
lunch->type$ = "Lunch"
lunch->protein$ = "chicken"
lunch->liquid$ = "coffee"
lunch->carb$ = "rice"
private routine show_one_meal with meal
print cluster meal
end routine
// call the routine
show_one_meal with meal breakfast
show_one_meal with meal lunch