Livecode Wiki
Advertisement

Function handlers are commonly called functions. LiveCode has many built-in Functions, a list of them is found at the end of this article. A programmer can create their own Functions, called Custom Functions. This article is mainly about how to structure and use Custom Functions,

A Custom Function is very similar to a Custom Message Handler. But there are differences, so don't confuse the two of them. However, which one you use usually comes down to personal preference. See also Message Handlers, sometimes called Commands.

Structure[]

The first line of a Custom Function must start with the word "function", followed by the name of the Function. That name is also the name of the message which causes its execution. If there are any function parameters, they are placed as a list of items after the name of the Function. The last line of a Function must start with the word "end", followed by the name of the Function.

Between the first line of the Function and the last are the list of statements that make up the Function. The statement list must include at least one Return statement. When the Function is executed, a Return statement must be executed. The first Return statement that is executed stops the Function execution and returns the value, it is the effective end of the Function.

A Function example:

function myWeek theDays                        -- myWeek has one parameter: theDays 
  return theDays + 7                           -- 7 is added to theDays, then it is
end myWeek                                     -- returned to the calling statement.

Calling Statement[]

The Calling Statement starts with the name of the Function. If there are any function parameters, they are placed as a list of items after the name of the Function in the Calling Statement. The parameter list in the Calling statement should match the parameter list in the Function in order. Each parameter in the parameter list of the Calling Statement must be an appropriate value or a variable containing the appropriate value. The parameter list in the Calling Statement must be enclosed in parenthesis. Use only one set of parenthesis.

The Calling Statement can be placed in any handler, which is then known as the Calling handler. When the Calling Statement is executed, the name and parameter list becomes the message which travels along the message path until it encounters the Function, which is then executed. The value that is returned by the Function is returned to the Calling Statement as if it was a variable. Therefore, the Calling Statement must be used like a variable in any of the LiveCode statements in the Calling handler.

A Calling handler for the above example function:

on mouseUp                             -- myWeek is called in this mouseUp handler.
  put myWeek (4) into thisWeek         -- thisWeek = 11
  answer "myWeek = " & thisWeek        -- answer shows myWeek = 11
end mouseUp                            -- end mouseUp handler

Another Function example:

function Fred george, bob, dude   -- george 1st number, bob the 2nd and dude the 3rd
  put george + bob - dude into Temp    -- Temp = 1st + 2nd - 3rd
  return Temp                          -- Temp is returned to the calling statement
end Fred                               -- end Fred function

The Calling handler for the above:

on mouseUp                               -- This is the calling handler mouseUp
  put Fred(4,5,6) into Susan             -- Fred = 3 and Susan = 3
end mouseUp                              -- end mouseUp handler

Private Functions[]

A Private Function is placed in the same script as the Calling handler. When LiveCode executes a Calling Statement, it sends the message to the local script first. If a Private Function is encountered, then LiveCode executes that Private Function. The message doesn't travel far.

The first line of a Private Function must start with the word "private", followed by the word "function", followed by the name of the Function. If there are any function parameters, they are placed as a list of items after the name of the Function. The parameter list of a Private Function operates the same as the parameter list of a Function.

Common Built-in Functions[]

Please help to complete this list

Advertisement