Understanding Sheerpower Routine Parameters
Sheerpower routines can handle up to 32 parameters in total, divided into two categories:
- Up to 16 parameters can be passed into routines.
- Up to 16 parameters can be returned from routines.
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.
Parameter Passing
Parameters Passed into a Routine:
These are passed by value, meaning their values cannot be altered by the routine,
with the exception of clusters.
Clusters are passed by reference, allowing the routine to modify the cluster,
such as adding new rows to a cluster array or changing cluster field values.
Strings Passed into a Routine:
Optimized for performance, so passing the same string multiple
times incurs virtually no performance penalty.
Returning Parameters:
New values are assigned by copying data into them.
Returning the same string multiple times is also optimized for performance, resulting in minimal impact.
Advantages of Named Parameters in Sheerpower Routines
- Clarity: Named parameters enhance code
readability by clearly indicating the purpose of each parameter.
- Order Independence: Parameters can be specified in any order,
providing flexibility in coding.
- Self-documenting Code: Named parameters serve as
inline documentation, reducing the need for extensive comments.
- Maintainability: Explicitly named parameters make it
easier to manage changes to parameter lists.
- Error Reduction: The risk of mixing up parameters is
minimized, reducing potential bugs.
Tip: For easier and clearer coding, name your
variables the same as the routine's parameters.
This allows you to call a routine using
just the variable names, in any order, and Sheerpower will
automatically match them to the corresponding parameters.
Example of Named Parameter Usage
routine calculate_tax with income, tax_rate, returning tax_amount
tax_amount = income * tax_rate
end routine
calculate_tax with income=50_000, tax_rate=0.2, returning tax_amount result
print "Tax Amount: "; result
// using implied parameter values
income = 50_000
tax_rate = 0.2
calculate_tax with income, tax_rate, returning tax_amount
print "Tax Amount: "; tax_amount
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 private 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, tax_rate=myrate, returning due mydue
private routine calculate_tax_due with amount, tax_rate, returning due
due = amount * tax_rate
end routine
Instead, we could use this code, taking advantage of optional "=".:
amount = 45
tax_rate = 5.6
calculate_tax_due with amount amount, tax_rate tax_rate, returning due due
and even further simplify it, taking advantage of implied parameters:
amount = 45
tax_rate = 5.6
calculate_tax_due with amount, tax_rate, returning due
These examples all produce equivalent results where the last example is the simplest.