Functions & Commands

Functions are blocks of code that we can execute when needed.

Functions follow the following format:

function NameOfFunction
 /* statements to
 execute */
end NameOfFunction

If the function will accept parameters, their names come after function name, separated by commas

function NameOfFunction param1, param2, param2
 /* statements to
 execute */
end NameOfFunction

Most functions will contain a return statement, that will return a value to the calling handler.

e.g 1

function helloWorld
 return "Hello World!"
end helloWorld
put helloWorld() // outputs: Hello World!

e.g. 2

function addTwoNumbers numberA, numberB
 return numberA + numberB
end addTwoNumbers
put addTwoNumbers(10, 5) // outputs 15

Passing Multiple Values

Some times you need a function to return multiple results:

function returnMulitpleVariables numberA
 return numberA+3, numberA+5
end returnMulitpleVariables
put returnMulitpleVariables(5) into myResult
put item 1 of myResult into numA // numA = 8
put item 2 of myResult into numB // numB = 10

If you need any of the returned variables to be a an array, you will need to use the arrayEncode() and arrayDecode() functions.

Hint: If you are not sure if your function names will conflict with LiveCode’s built in functions, you can display a list of all the functions with: put the functionNames

Leave a Reply