|
Routine Parameters in Detail |
Sheerpower routines can handle up to 32 parameters in total, split into two categories:
The with clause names the parameters passed into the routine, and the returning clause names the parameters returned from it.
dynamic to keep it open.
By default, all WITH parameters in Sheerpower are passed into a routine by
reference and are treated as read-only.
This means the routine works directly with the original data, rather than a copy.
As a result, parameter passing is highly efficient, no matter how large or complex
the data being passed.
Unless explicitly given a type, routine parameters are initially
dynamically typed. A dynamic parameter can accept any
supported type: STRING, REAL, BOOLEAN,
INTEGER, or a custom type. The first time the parameter is used
in an expression or operation, the system infers and fixes its type for all
subsequent uses within the program. The exception is when the argument
passed in was explicitly declared as dynamic; in that case,
the parameter remains dynamic across all uses.
To enforce a specific type, declare the type name followed by the parameter names. Types are sticky: once a type appears, it applies to the listed parameter and any that follow until another type is given.
In this example, a, b, and c are all
fixed as STRING parameters.
dynamic, in which case it stays dynamic).
?? Inconsistent argument types were used. -- Argument "DO_TAXES$AMOUNT": Expected Real, got String
By default, values returned from a routine are copied into the named returning parameters. Returning the same string multiple times is optimized internally, so performance remains fast.
Clusters as returning parameters are passed by reference. This lets routines add rows to cluster arrays or update existing fields directly. Any changes made inside the routine are reflected in the original cluster.
with parameters directly.
This was allowed back when the default calling method was by value.
To preserve this behavior, enable OPTION LEGACY at the top of your program.
With this option turned on, all with parameters are passed by value
(copied), so they could be changed locally without affecting the caller's original arguments.
OPTION LEGACY also preserves the old omitted-parameter
behavior: a call may leave out a parameter that has no default value, and the
parameter simply keeps its value from the previous call. (Without the option,
such an omission is a compile-time error — see Default Parameter Values
below.) Parameters that do have defaults still fill in as usual.
Tip: For clarity, use variable names that match the routine's parameter names. This allows you to call the routine with just the variables, in any order, and Sheerpower will automatically match them.
Routines can also be called without any parameters. This is typical for global routines. Private and scoped routines almost always take parameters.
There can be up to 16 parameters in the with clause and up to another 16 in the returning clause. For maintainability, keep the number of parameters small. When large amounts of data must be exchanged, use cluster parameters (see next tutorial).
Passing large data (like a 4 MB file or an entire Bible text) is as fast as passing a single number, because only a reference is passed, not the data itself.
Sheerpower's parameter model combines the speed of pass-by-reference with compile-time safety, making it ideal for large-scale business applications that demand both performance and reliability.
There are three increasingly concise ways to pass parameters:
All three are equivalent—the last version being the simplest.
There is one more step in concision: a with parameter can declare
a default value, and callers simply omit it. Callers that
pass the parameter override the default:
The default is an expression, evaluated at every call that omits the parameter — not once when the routine is defined. That means defaults can do real work:
Order matters when one parameter's default uses another: declare the
parameter being referenced before the parameter that uses it
— length first, then width = length.
Names in a default expression are resolved left to right, so if a default
names a parameter that only appears later in the list,
Sheerpower quietly uses the module variable of that name
instead. That is not a compile error — just a different value than
you meant — so keep referenced parameters on the left.
Defaults work the same whether the routine is called as a statement or as a function:
If a parameter has no default, every call must pass it. Omitting it is a compile-time error, reported at the exact call site:
This check protects you from a subtle bug: without it, an omitted parameter
would silently keep its value from the previous call. A routine
whose parameters all have defaults can be called bare, with no
with clause at all.
returning parameters cannot have default values.
A routine can take a function as one of its parameters.
This lets the code that calls the routine choose which function to use.
You write the function keyword and empty parentheses, then
call it inside the routine like any other function:
When you call the routine, you pass the function the same way — the
function keyword and the function name with ():
The same idea works for text. This routine uses whatever string function you give it:
A routine that returns one value can also be called as a function, or with the pipe. So the same routine works in all three ways:
The function you pass can be one of your own routines, not just a built-in. It can take one value, or several. This routine takes two values and passes them to whatever function you give it:
You can put function fn() anywhere in the parameter list.
Both with a, b, function cmp() and with function cmp(), a,
b work. And when you call a routine as a function, the first value may
skip its name: apply_pair(30, b = 10) means the same as
apply_pair(a = 30, b = 10).
Passing a function lets you write a routine once, then let the code that calls it decide one step. Two everyday examples:
Build a line of output, and let the caller choose how to style the value:
Or do a fixed calculation, and let the caller choose how to round the result:
In both, the routine holds the fixed work — building the line, adding the tax — and the function you pass fills in the one part that changes.
sqr(),
ucase$(), or trim$()) or one of your own
routines. It can take one value or several. Inside the routine you
call it the normal way: fn(x), or fn(a, b) for two
values.
function fn() first, last, or in the middle of the parameter
list.
function parameter, and every call must pass the same
function. To use a different function, write a second routine.
with clause (inputs) and 16 in the
returning clause (outputs).
with parameters are passed
by reference and treated as read-only,
providing both speed and compile-time safety.
typeof$() and dtype().
=, explicit naming without =,
and fully implied parameters — all equivalent, with the last being the simplest.
with parameter can declare a default value
(surcharge = 10), evaluated fresh at every call that omits it.
Parameters without defaults are required — omitting one
is a compile-time error.
function fn()): the caller passes a built-in or a
routine, and the body calls it as fn(x). One distinct
function per routine in this version.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |