Functions

A function is a type of handler that is a source of value. SenseTalk provides many functions for a wide variety of purposes. Some functions may produce a different value each time they are used, such as the time which provides the current time of day. Other functions may produce a different value depending on what parameters are passed to them—using the same function with different parameter values will produce different results. For information on passing parameters, see Parameters and Results.

The functions that are built-in as part of the SenseTalk language are described throughout this manual, especially in the Commands and Functions section. The host application environment in which SenseTalk is running may provide other predefined functions. In addition, you can write your own functions that can be used by your scripts. See Handlers for more information.

Calling Functions

To use a function value, a script "calls" that function, either on its own or as part of an expression. SenseTalk provides several different ways to call a function—you can use whichever one seems most natural in a given situation. The simplest type of function call is one without parameters that is not being sent to a particular object. Such a function call can be made in two ways: either by naming the function followed by an empty pair of parentheses (indicating no parameters); or by using the word "the" followed by the function name:

put date() -- displays the date

put the date -- displays the date

To call a function with a single parameter, either include that parameter in the parentheses after the function name, or follow the function name with "of" and the parameter:

put length("abcd") -- 4

put the length of "abcd" -- 4

In the second case above, because "of" is used, SenseTalk is able to recognize it as a function call even without the word "the":

put length of "abcd" -- 4

Because many functions (such as the "length" function) calculate some attribute of a value, SenseTalk also allows the property access notation to be used to call a function:

put "abcd"'s length -- 4

put "abcd".length -- 4

Calling a function with more than one parameter is similar to the one-parameter case. All of the following will pass 4 parameters to the average function:

put average(2,4,6,8) -- 5

put the average of 2 with (4,6,8) -- 5

put 2's average(4,6,8) -- 5

put 2 .average(4,6,8) -- 5

Although some of these seem a bit odd in this case, there may be other situations where those forms will seem more natural. (Note that in the last example above, a space is needed after the number 2 to prevent the period from being interpreted as a decimal point.)

The name of a function to call can be generated dynamically by using an expression in parentheses in place of a function name:

set funcToCall to "length" -- name of the function

put (funcToCall) of "abcd" -- 4

A list of functions can be called on the same value at once, producing a corresponding list of results:

put [length, uppercase, lowercase] of "Test" -- [4,"TEST","test"]

Example

Here is an example function handler that takes one parameter (aWord) and returns a modified copy of its value:

function plural aWord

if aWord ends with "s" then put "es" after aWord

else if aWord ends with "y" then put "ies" into the last char of aWord

else put "s" after aWord

return aWord

end plural

Here is an example showing how this function could be called from a script:

ask "What type of object are you carrying?"

put it into itemType

ask "How many do you have?"

put it into howMany

if howMany is not 1 then put the plural of itemType into itemType

answer "You have " & howMany && itemType

 

This topic was last updated on August 19, 2021, at 03:30:51 PM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2022 Eggplant