FANDOM


Functions and handlers have a lot in common, they both execute statements when called. But the thing that sets apart a Function is that it returns a value. There are many built-in functions in LiveCode and you can write your own custom function.

For 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.

And now the calling statement:

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

A custom function must start with the word "function" and end with the word "end". The name of the function must be the second word of the first statement and the last statement. If there are any parameters, they are listed after the name of the first statement, separated by commas. its possible that there are no parameters, in which case that part of the first statement will be blank. One or more statements must be placed between the first statement and the last. One of those statements must be a Return. It is the effective end of the function, any statements after a Return are ignored by LiveCode.

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

To use a Function, it must be called from another handler or function. The calling statement can be most any LiveCode statement. Any parameters in the calling statement must be placed inside parenthesis, separated by commas. The order of the parameters in the calling statement are the same as in the function being called. Use the same number of parameters in the calling statement as in the function being called.

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

But do not use additional parenthesis. If you do, the parenthesis will be evaluated as a single parameter. For example:

function mouseUp
  put ReturnContact((Bill,Howard),Phoenix) into tName
end mouseUp 
function ReturnContact firstName,LastName,address
 return firstName
end ReturnContact

ReturnContact only gets two parameters, firstName = Bill,Howard and LastName = Phoenix. firstName is returned, so tName = Bill,Howard because these are evaluated as param 1 due to the parentheses.

Here is a list of common LiveCode built-in functions: (pleas help us to complete this list)

Community content is available under CC-BY-SA unless otherwise noted.