Functions in Hypertalk


Function handlers in Hypertalk behave in the same way that mathematical functions do. You are familiar with mathematical functions such as AVERAGE(), ABS() or SQRT(). If we have numerical variables X and Y,

X = SQRT(Y)

will assign the square root of Y to X.

In a programming language which supports the SQRT function, such a statement would:

  1. compute the square root of Y by calling a function called SQRT
  2. assign the resulting value to X
In this case, Y may be referred to as a parameter or argument. In general, a function may occur in any expression which computes a value to be placed in a container (variable, or field) or any expression which is used to control the flow of execution of a program. And the function behaves, in that expression, as though it were a variable or constant.

Thus, for example:

Most programming languages allow the programmer to define her or his own functions. The 'No Account' stack includes a function called FindMin which is defined as part of the 'Do it!' button script.

*** Function Parameters ***

Note that in the definition of the function FindMin, the first statement is

function FindMin startingline, WhichWord

and that startingline and WhichWord are parameters.

Now find where the function FindMin is referenced in the mouseup handler of the 'Do it!' button script:

put FindMin(current, whichWord) into found

In this statement, current is the number of the current line and whichWord is the number of the word used as the basis of the sort. The values of the parameters current and whichWord are set in the MouseUp handler. When FindMin is referenced in the MouseUp handler, the values contained in these variables are channeled into the function handler through its defined parameters startingline and whichWord. Thus, the values contained in current and whichWord are passed to function FindMin through the parameters startingline and whichWord.

*** Functions Return Values ***

Functions are said to "return" values that are determined by some sort of computation. Thus sqrt(X) will return the square root of X, where X is a numerical parameter.

The function FindMin (in the 'Do it!' button script) returns the value associated with the variable where (note the last line of the function definition). What type of value does where contain? If you examine the FindMin script, you will discover that where is assigned a line number, namely the number of the line which has the current minimum value of the word WhichWord. Thus the statement:

put FindMin(current, whichWord) into found

in the MouseUp handler assigns the number of the line that contains the minimum (up till now) value of WhichWord to the variable found.

*** The Type of a Function ***

A function can be of any data type. The type of a function is determined by the data type that it returns. A function can be used in an expression so long as it is of the right type. The functions sqrt() and abs(), for example, are numerical types and so can be used in mathematical expressions.

Recall that the logical (or boolean) data values are 'true' and 'false'. It is possible, therefore, to define functions that are of type logical.

*** The Logical Function FormatOK ***

In lab exercise 7.2 if Module 4, you are asked to script a function called FormatOK. The function will examine a line of data and make sure it contains four words. If it does, the function will return 'true', otherwise it will return 'false'. Thus FormatOK will be of type logical, and can be included in any boolean or logical expression, for example:

  if FormatOK(thisline) then
      -- process this line of data

FormatOK may be defined as follows:

      function FormatOK dataline       -- dataline is the parameter
            if number of words of dataline = 4 then
                  return true
            else
                  return false
            end if
      end FormatOK

We can use FormatOK to check whether a line of 'data' has 4 words, and if it does, then we can process the line of data.

You may now practice using the function. First determine where the FormatOK function should be defined in the No Account stack and define it. Now write statements in some other handlers to test it. All of the button scripts that manipulate the data lines in card field 'data' should have access to this function. Before they manipulate a line of data you can use FormatOK to check that line. For example:

      put number of lines of card field data into numlines
      repeat with count = 1 to numlines
            put line count of card field data into thisline
            if not FormatOK(thisline) then
                  answer "Bad data format" with "Okay"
            else
                  -- process this line of data
            end if
      end repeat

Of course, to really test this function, card field 'data' must contain some ill-formatted data lines. Make sure you update card field 'data' with one or more lines that do not contain four words.