Home of LDMud
Functions are code blocks that have a name. Every LPC code that wants to be executed must be somewhere within a function. (There is just one exception: the initialization of global variables.)
A function has a name, a return type, arguments (i.e. values given to the function when called) and its code block:
int get_square(int x)
{
return x*x;
}
In this example the first int denotes the return type.
It's followed by the function name and the arguments in parenthesis.
After that comes the code block in braces.
To call a function within the object just write the name and the values for its arguments in parenthesis,
x = get_square(42);
To call a function in another object there is an efun
call_other which can be abbreviated with ->.
x = ob->get_square(42);
x = call_other(ob, "get_square", 42);Functions and global variables can have modifiers to change the behaviour of the function resp. variable. The modifier is written just before the return type.
privateprotectedcall_other (from this object or another object)
will ignore this function.
staticcall_other) will ignore
this function.
publicnomaskdeprecatedvarargs0. Please note that calls
via call_other are always treated as
varargs calls.
nosavesave_object or restored
by restore_object
async to be asynchronous.
This means the function can suspend its execution at any time and be resumed later on.
At each suspension a value can be passed in and/or out.
async void fun()
{
// Do something.
int a = yield(); // Suspend and get a value from outside.
// Do something else.
int b = yield(); // Suspend and get another value.
return a+b;
}call_coroutine
or using await, the execution can be started/continued.
coroutine cr = fun();
call_coroutine(cr); // Initial start
call_coroutine(cr, 1); // Pass 1 into the coroutine
int result = call_coroutine(cr, 2); // Pass 2 into the coroutinecall_coroutine
or await. The former suspends execution only till the first suspension
of the called coroutine, the later suspends execution until the called coroutine has ended.
async void fun()
{
foreach(int i: 10)
{
printf("Counting: %d\n", i);
// Assuming there is a sleep() coroutine.
await(sleep(1));
}
}