Manual:DIL Manual/return

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:25, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

return

Instruction.

return;
return (expression);

Ends the template that is running and goes back to the one that called it, which carries on after the call. A procedure (a template without a return type) uses return;. A function (dilbegin integer name(...), dilbegin string ...) must use return (value);, and the call gives that value.

Notes

  • vmc checks the form: return; in a function gives the error "no return expression expected" (meaning one is required), and return (x); in a procedure, or with a value of another type, gives "return expression not of correct type".
  • The value must have exactly the template's type. return (null); doesn't compile in a unitptr function; return a unit variable that is null instead.
  • In a program that wasn't called by another (one started by dilcopy or on a unit), return ends the program, as quit does.
  • Returning ends the template's secure()s and interrupt()s, and frees its local variables. A string, stringlist or intlist value is copied to the caller.
  • A function's result can't be passed straight into another DIL template call as an argument; put it in a variable first. Built-in functions such as itoa() take it directly.

Example

dilbegin unitptr firstpc(room : unitptr);
var
   u : unitptr;
code
{
   u := room.inside;
   while (u != null)
   {
      if (u.type == UNIT_ST_PC)
         return (u);
      u := u.next;
   }
   return (u);
}
dilend

dilbegin greetpc(u : unitptr);
code
{
   if (u == null)
      return;
   act("Welcome, $1n.", A_SOMEONE, u, null, null, TO_CHAR);
   return;
}
dilend

dilbegin greeter();
external
   unitptr firstpc@ex(room : unitptr);
   greetpc@ex(u : unitptr);
var
   pc : unitptr;
code
{
   heartbeat := PULSE_SEC * 10;

   :start:
   pause;
   pc := firstpc@ex(self.outside);
   greetpc@ex(pc);
   goto start;
}
dilend

A function that returns the first player in a room (or null), and a procedure that returns early when there is nobody to greet.

See also