Language Guide
CHAPTER 8
Handlers
Using Subroutines 227
--the factorial() subroutine returns the factorial of a number
on factorial(x)
if x > 0 then
return x * (factorial(x - 1))
else
return 1
end if
end factorial
--the min() subroutine returns the smallest number in a list
on min(numberList)
if class of numberList list or numberList = {} then
return numberList
if length of numberList = 1 then return item 1 of numberList
copy item 1 of numberList to frontNumber
copy length of numberList to listLength
copy min(items 2 thru listLength of numberList) to tailNumber
if frontNumber > tailNumber then
return tailNumber
else
return frontNumber
end if
end min
To save this script as a compiled script, choose Save As from the Script Editor’s
File menu and choose Compiled Script from the Kind pop-up menu. Then save
the script as a file called Numeric Operations. (If you are using a different
script editor, see the documentation that came with it.)
After you save the script as a compiled script, use the Load Script scripting
addition command to make the subroutines it contains available in the current
script. For example, the Load Script command in the following script assigns
the compiled script Numeric Operations to the variable NumberLib. To call the
subroutines in Numeric Operations, use a Tell statement. The Tell statement in