Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 8 - Handlers / Subroutine Definitions and Calls
Subroutines With Positional Parameters

Examples of Subroutines With Positional Parameters

Here is a subroutine that returns the minimum value of a pair of values followed by an example of how to call the subroutine.

on minimumValue(x, y)
   if x � y then
      return x
      return y
   end if
end minimumValue

minimumValue(21, 40000)
You can also define a subroutine whose positional parameters define a pattern to match when calling the subroutine. For example, the subroutine that follows takes a single parameter whose pattern consists of a list of two items in a list.

on point({x, y})
   display dialog ("x = " & x & ", y = " & y)
end point

set mypoint to {3, 8}
A parameter pattern can be much more complex than a single list. The handler in the next example takes two numbers and a record whose properties include a list of bounds and displays a dialog box summarizing some of that information:

on hello(a, b, {length:l, bounds:{x, y, w, h}, name:n})
   set q to a � b
   set response to "Hello " & n & ", you are " & l & �
      " inches tall and occupy  position (" & x & ", " & y & ")."
   display dialog response
end hello

set thing to {bounds:{1, 2, 4, 5}, name:"George", length:72}
hello (2, 3, thing)
As you can see from this example, a call to a subroutine with patterned parameters can include parameters that aren't literals, as long as they evaluate to the appropriate pattern. Similarly, the properties of a record passed to a subroutine with patterned parameters don't have to be given in the same order in which they are given in the subroutine's definition, as long as all the properties required to fit the pattern are present.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996