|
H.2 Passing Simple Parameters to Routines
|
Sheerpower routines can have zero to 16 parameters. Parameters are always
passed by name. There are two types of parameters: 1) those passed into the
routine, and 2) those that are returned from the routine. Routines can also be
called without any parameters at all. This is typical for
global
routines. Private and scoped routines tend to always have parameters.
Here is an example of a routine that calculates tax. We will pass into it an
amount and a tax rate. It will return the tax due.
profit = 1234
tax_rate = 6.5/100
calculate_tax_due with amount profit, rate tax_rate, returning due tax_due
print 'Tax due: '; tax_due
private routine calculate_tax_due with amount, rate, returning due
due = amount * rate
end routine
The
with clause contains the names of the parameters coming into the routine. The
returning
clause contains the names of the parameters returned by the routine.
There can be up to 16 parameters in the
with clause and up to another
16 parameters in the
returning clause. From a maintenance standpoint,
it is recommended to keep the number of parameters to just a few. If you need
to pass in or out a lot of data, use
cluster parameters (see next
tutorial).
Simplified routine parameters
When passing parameters to routines, often there is a pattern of coding:
myamount = 45
myrate = 5.6
calculate_tax_due with amount myamount, rate myrate, returning due mydue
private routine calculate_tax_due with amount, rate, returning due
due = amount * rate
end routine
Instead, we could use this code:
amount = 45
rate = 5.6
calculate_tax_due with amount amount, rate rate, returning due due
and even further simplify it as:
amount = 45
rate = 5.6
calculate_tax_due with amount, rate, returning due
The examples produce equivalent results where the last example is the simplest.