Home
The HyperCard Center
HyperTalk Reference
Home » HyperTalk Reference » Keywords

Note: This is a work in progress and will be formatting errors. Read more about the project on the home page.

function

function functionName [parameterList]
  statements
end functionName


The function keyword defines a new function handler of the specified name. You call a function by placing parentheses after its name, enclosing any parameters within the parentheses:


get deleteSpaces(" hello ")


The optional parameterList lets a function handler receive values sent along with the function call.


When a function is called, HyperCard evaluates each item within the parenthetical list following the function's name. When the handler begins to execute, HyperCard assigns each value to a parameter variable in the parameterList.


Use the return keyword within the function definition to have the function return a value to the handler that called it. If you don't use return, the function evaluates to empty.


Demo Script

on mouseUp
   ask "What is your name?"
   if it is empty then exit mouseUp
   answer "Your name spelled backwards is" && reverseString(it) & "."
 end mouseUp
 

 function reverseString theString
   repeat with i = the number of chars in theString down to 1
     put char i of theString after theReversedString
   end repeat
   return theReversedString
 end reverseString

Placeholders

functionName
Any text string, without quotation marks, that represents the name of the function handler you want to write:

myAverage
onlyOneWindow
As in the HyperTalk examples:
function myAverage theNumbers
function onlyOneWindow


parameterList
A comma-separated list of local variable names.

The actual names don’t matter as long as they’re not the same as one of HyperCard’s reserved words. (That is, don’t use result as a variable name.) For example:

var1
var1,var2,var3, var4
fieldName, N, numberOfCharacters


statements
Any return-separated list of built-in commands, user-defined handlers, or keywords that are part of a message or function handler.

put "Hello world"   -- built-in command
get total(field 1)  -- function call
global HelpInfo     -- keyword


Related Topics

« exit | HyperTalk Reference | global »

Version 0.7b1 (March 24, 2022)

Made with Macintosh

Switch to Retro View