This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathfunction.lcdoc
More file actions
119 lines (93 loc) · 4.06 KB
/
function.lcdoc
File metadata and controls
119 lines (93 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Name: function
Type: control structure
Syntax:
[private] function <functionName> [<parametersList>]
<statementList>
end <functionName>
Summary:
Defines a custom function <handler>.
Introduced: 1.0
OS: mac, windows, linux, ios, android, html5
Platforms: desktop, server, mobile
Example:
function myFunction
return "test"
end myFunction
Example:
function cubeIt pNum
return pNum * pNum * pNum
end cubeIt
Parameters:
functionName (string):
A string up to 65,535 characters in length.
parametersList:
The parametersList consists of one or more parameter names, separated by
commas.
statementList:
The statementList consists of one or more LiveCode <statement|statements>.
The final <statement> is typically a <return(control structure)> <statement>,
which sends the value derived in the <function> <handler> back to the
<statement> that <caller|calls> the function.
The result:
By default the <result> is set to the value <return(glossary)|returned> by
the function. See entry for the <return(control structure)>
<control structure> for more details.
>*Note:* As the <result> function is global in scope, if you do not
> explicitly <return(glossary)> a value, then the <result>
> will not change, but reflect the last engine command, engine function
> or command or function handler that did <return(glossary)> a value.
Description:
Use the <function> <control structure> to implement a custom function.
**Form.** The first line of a <function> <handler> consists of the word
"function" followed by the function's name. If the function has any
<parameter|parameters>, their names come after the function name,
separated by commas.
The last line of a <function> <handler> consists of the word "end"
followed by the function's name.
The purpose of a function is to compute a value and <return(glossary)>
it to the <handler> that called the function. The function's value is
<return(glossary)|returned> by a <return(control structure)>
<control structure> within the <function> <handler>.
A <function> <handler> can contain any set of LiveCode <statement|statements>.
Most functions contain a <return(control structure)> <statement>, which
<return(glossary)|returns> the value to the <caller|calling handler>.
This example of a custom function uses two <parameter|parameters> and
<return(glossary)|returns> a <string> :
function reversedName firstName,lastName
-- firstName and lastName are parameters
put lastName,firstName into constructedName
return constructedName
end reversedName
You create a custom function by writing a <function> <handler> for it. When
you <call> the function in a <handler>, the function <call> is passed through
the <message path>. When it reaches the object whose script contains the
<function> <handler>, the <statement|statements> in the <handler> are
<execute|executed>.
A custom function is called by name, just like a built-in function, as
part of an <expression>. For example, this handler calls the custom
function above:
on mouseUp
ask "What is your first name?"
put it into firstParam
ask "What is your last name?"
put it into secondParam
put reversedName(firstParam,secondParam) into field "Name"
end mouseUp
A function can call itself. The following example calls itself to
compute the factorial of an integer:
function factorial theNumber
if theNumber = 1 then return 1
else return theNumber * factorial(theNumber -1)
end factorial
>*Note:* To declare a function that is local to the script it is
> contained in, prefix the declaration with <private>. For more
> information about this see the dictionary entry for the <private>
> keyword.
References: $ (keyword), call (glossary), caller (glossary),
control structure (glossary), end (keyword), execute (glossary),
exit (control structure), expression (glossary),
functionNames (function), handler (glossary),
message path (glossary), param (function), paramCount (function),
parameter (glossary), private (keyword), result (function),
return (control structure), return (glossary), statement (glossary),
string (keyword)