Parameters and Results
Messages are identified by a single word — the message name — but they can also include additional information in the form of parameters. Parameters are values that are passed to commands or functions. These commands or functions may be custom handlers, which can either be located within the calling script, or in another script or helper suite. For more information on declaring handlers and receiving parameters, see Handlers
Two Ways to Pass Parameters: Sequential and Named
There are two ways to pass parameters when calling a command or function: sequentially, or by name. Sequential parameters are passed in sequence, as you might expect. Named parameters are passed as a property list of key:value pairs, with each key being used to match the name of a parameter on the receiving end. The two methods of passing parameters can also be combined.
The calling command or function can be in the same or a different script from the handler being called. The call is often in a separate script, which is helpful for the modularization of tests. A test structure might include a primary script with a number of different calls in it out to other scripts containing a wide variety of handlers that execute different tasks.
If you are interested in pre-defining a default value for any parameter the receiving handler might expect, see Default Parameter Values. For information on writing handlers to receive parameters, see Handlers.
Sequential Parameters
Passing Sequential Parameters
To pass parameters sequentially, simply list the parameter values after the command name separated by commas, or enclosed in parentheses after the function name when calling a function (there are some other variations for functions when only a single parameter value is being passed — see Calling Functions).
The command put "Hello, World!" sends the string “Hello, World!” as a single parameter along with the put
message. To send more than one parameter, just list them with commas in between.
Example: Passing Sequential Parameters with a Command
This example passes three parameters to the doSomethingImportant
command as sequential parameters: the number 31
, the text green
, and the variable style
:
doSomethingImportant 31, "green", style
Command parameters should not be enclosed in parentheses (unless the intent is to pass a list as a single parameter).
Example: Passing Sequential Parameters with a Function
To include parameters with a function call message, list them inside the parentheses after the function name:
put roundToNearest(salary/12, 0.01) into monthlyPayTothePenny
Example: Supplying an Empty Parameter
Two commas in a row imply an empty parameter, and a final comma at the end of a list is ignored, so the following passes three parameters ("silverBar"
, ""
, and 16
):
get verifyQuantity("silverBar",,16,)
Sequential Parameter Assignment
When parameters are passed sequentially, their values are assigned (in the same order) to any parameter variables that are part of the handler declaration. If the caller passes fewer values than the number of parameter variables, those that aren’t assigned will receive their default values (or empty). If the caller passes more values, the handler can access them using the parameterList
function, the paramCount
function and the param
function.
Examples:
For example, consider a castSpell
handler declared like this:
to castSpell spellName, duration, potency -- the castSpell handler declares three parameter variables
//Do things to cast the spell:
Put duration and potency into Cauldron
Stir Cauldron -- A custom call that executes the spell
Log "Alakazam!: " & spellName -- A message confirming that the spell has been cast
end castSpell
Here, spellName
, duration
, and potency
are the parameter variables that will receive the values of parameters that are passed when the handler is called.
If the above handler is called using the command castSpell "sleep", 12 hours, "deep"
, then the whole script might look like this:
castSpell "sleep", 12 hours, "deep" --This is the calling command, passing three parameters sequentially
to castSpell spellName, duration, potency --The sequentially passed parameters are received in order and inserted into their corresponding parameter variables
//Do things to cast the spell:
Put duration and potency into Cauldron
Stir Cauldron -- A custom call that executes the spell
Log "Alakazam!: " & spellName -- A message confirming that the spell has been cast
end castSpell
In this case, the value “sleep” will be assigned to the parameter variable spellName
, the value 12 hours will be assigned to duration
and “deep” will be assigned to the potency
variable.
Suppose the same handler is called using the command castSpell "sleep"
:
castSpell "sleep"
to castSpell spellName, duration, potency
//Do things to cast a spell!
TypeText "Alakazam!"
end castSpell