Function definition: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Changed to Wren S/H)
imported>RonReu
Line 419: Line 419:
end</syntaxhighlight>
end</syntaxhighlight>


A function in AppleScript is called a "handler". It can take one of three different forms, depending on what the scripter finds most convenient. Calls to it must match the form used in the handler definition. The above is an example of a handler with "positional" parameters. Either <code>to</code> or <code>on</code> may be used as the first word in the header line. When the script's compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.
A function in AppleScript is called a "handler". It can take one of three different forms, depending on what the scripter finds most convenient. Calls to it must match the form used in the handler definition. Either <code>to</code> or <code>on</code> may be used as the first word in the handler definition. When the script is compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.

Handler names followed by zero or more parameters within parentheses are called "positional" -- the number and order of the parameters in the caller must match those in the handler definition.


<syntaxhighlight lang="applescript">on multiply(a, b)
<syntaxhighlight lang="applescript">on multiply(a, b)
Line 427: Line 429:
multiply(2, 3)</syntaxhighlight>
multiply(2, 3)</syntaxhighlight>


AppleScript also offers handlers with "labeled" [sic] parameters. These aren't used much now as the limited choice of label enums makes it difficult to choose ones that make sense in English, although it's just about possible here:
AppleScript also offers handlers with "prepositional" labeled parameters. These aren't used often because the set of AppleScript-defined prepositions makes it difficult to choose ones that make sense in English.

These prepositions can be used: <code>about, above, against, apart from, around, aside from, at, below, beneath, beside, between, by, for, from, instead of, into, on, onto, out of, over, since, thru, through, and under</code>. Also, <code>of</code> is also available, but if used it must be the first parameter.

Example:


<syntaxhighlight lang="applescript">on multiplication of a by b
<syntaxhighlight lang="applescript">on multiplication of a by b