blob: 06949ea5ddf3966031ac808d2f42d480d983678e [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001CONCEPT
2 functions
3
4DESCRIPTION
5 Functions are named blocks of code which are be called with
6 a number of argument values, and which return a result value
7 to the caller.
8
9 Functions are defined in an object and are also known as
10 "local funs" or short "lfuns".
11
12
13 DEFINING A FUNCTION
14
15 A function definition takes the form
16
17 <modifiers> <type> name ( <arguments> ) {
18 statements...
19 }
20
21 The parts in detail:
22 - <modifiers> can be any one of "static", "private", "public"
23 and "protected" (see modifiers(LPC)), optionally combined
24 with "varargs" (see varargs(LPC)) and/or "nomask".
25 If not specified, the function behaves as if it was
26 specified as "public", but this visibility can be restricted
27 in derived object through non-public inheritance.
28 - <type> is the type of the result returned by the function.
29 If specified as "void", the function is compiled to return
30 the value 0 under all circumstances. If not specified, the
31 type is assumed to be "mixed", furthermore typechecking is
32 disabled for this function.
33 - name is the name of the function, e.g. "short", or "Nice_Try",
34 under which it is made known.
35 - <arguments> is a list of variable definitions in the
Zesstra98e90af2021-05-07 19:18:04 +020036 normal '<type> <name> = <default value>' style, separated
37 by comma.
MG Mud User88f12472016-06-24 23:31:02 +020038 Examples: () : no argument taken
39 (int a) : takes on integer argument
40 (mixed a, object *b): takes two arguments, one
41 arbitrary type, one array of objects.
Zesstra98e90af2021-05-07 19:18:04 +020042 (int a = 42) : takes zero or one integer argument.
43 if none is given, 42 is used instead.
44 If default values are given, they must be given for all
45 following arguments except a varargs argument. If a function
46 has a prototype, the default values should be specified
47 there instead of the definition (otherwise it might not
48 be known that these arguments are optional).
MG Mud User88f12472016-06-24 23:31:02 +020049 - { statements... } defines the code for this function. This
50 is a normal block (see block(LPC)) and as such can define
51 its own local variables.
52
53
54 DECLARING A FUNCTION
55
56 A function declaration makes the name and type of a function known
57 to the compiler with the assertion that the code for this function
58 will be provided "elsewhere".
59
60 The form is:
61
62 <modifiers> <type> name ( <arguments> );
63
64 Typical uses are:
65 - to declare in advance functions which are called before they
66 can be defined; for example if the create() function of an object
67 calls other functions which are defined after the create().
68 - to declare functions which will be provided by an inheriting
69 object.
70
71 Calling a declared but undefined function results in a runtime error.
72
73
74 CALLING A FUNCTION
75
76 Functions in other objects are called with the call_other() efun,
77 which can be shortened to '->':
78
79 ob->fun(a, b, c)
80 call_other(ob, "fun", a, b, c)
81
82 Note: See the entry H_DEFAULT_METHOD in hooks(C) for a modification
83 of this behaviour.
84
85 Functions in the same object are called just by writing their name,
86 followed by the arguments in parenthesis:
87
88 short()
89 compute(a)
90 do_that(a, "foo")
91
92 Array function arguments can be 'flattened' with the '...' operator.
93 For example:
94
95 mixed * m = ({ "foo", "bar" });
96 fun(m..., 1);
97
98 will be executed as:
99
100 fun("foo", "bar", 1);
101
102
103 If the number of values passed to the function does not match the
104 number of expected arguments (and if type checking is not enabled), the
105 driver will perform the necessary adaption at call time: excess
106 values are ignored, missing values are substituted by the number 0.
107 The values passed to the called function are massaged by the driver
108 to match the argument list
109
110
111 FUNCTIONS AND INHERITANCE
112
113 A "public" or "protected" (== "static") function defined in one
114 object is also visible in all inheriting objects. The exception from
115 this rule is when an inheriting child redefines ("overloads") the
116 inherited function with its own. When compiling with type checking,
117 the argument list of the redefined function has to match the
118 original one.
119
120 When a function is called, the driver looks for the function first
121 in the object called, and if not found there, then in the inherited
122 objects.
123
124 To explicitely call an inherited function (useful when a redefining
125 functions wants to use the original one), the "::" operator is used:
126
127 ::create()
128 ::compute(a)
129
130 The named function is searched only in the inherited objects, and
131 the first found is used.
132
133
134 If the function is inherited from several objects and a specific
135 one is to be called, the "::" can be extended to contain the
136 partial or full name of the inherited object:
137
138 inherit "/obj/cooker";
139 inherit "/obj/container";
140
141 tainer::create()
142 container::create()
143 "tainer"::create()
144 "container"::create()
145 "obj/container"::create()
146 "/obj/container"::create()
147
148 all call the create() in the container inherit. Note that the
149 name given to the :: operator is matched against the ends of
150 the inherited names.
151
152
153 One special form of this call is
154
155 efun::find_object()
156
157 which bypasses any redefinition of an efun (here find_object())
158 and directly calls the efun itself. This is only possible for
159 efun-redefinitions which do not use the "nomask" modifier.
160
161
162 Additionally, a call to a function inherited from several objects
163 can be instructed to call _all_ inherited functions through the
164 use of the wildcards "*" (match any number of arbitrary characters)
165 and "?" (match one arbitrary character):
166
167 inherit "/obj/cooker";
168 inherit "/obj/container";
169
170 "*"::create()
171 "co*"::create()
172 "*er"::create()
173
174 all call both inherited create()s. The function called this way
175 must not take arguments, and the single results from all calls are
176 combined into one array used as final result. If there is no such
177 function inherited at all, the statement will just return
178 an empty array.
179
Zesstra98e90af2021-05-07 19:18:04 +0200180HISTORY
181 LDMud 3.6.4 introduced default values for function arguments.
182
MG Mud User88f12472016-06-24 23:31:02 +0200183SEE ALSO
184 types(LPC), modifiers(LPC), varargs(LPC), references(LPC),
185 call_other(E), simul_efun(C), call_out(E)