Difference between revisions of "Manual:DIL Manual/return"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="fret"></span>
+
= return =
−
return:
+
'''Instruction.'''
−
  Return from a call to a procedure template (no return type
 
−
  declared). The execution continues from where the procedure
 
−
  was called.
 
  
−
  ---~---~---~---~---~---~---~---~---
+
  return;
−
<span id="fret"></span>
+
  return (expression);
−
  return:
 
−
  Return from a call to a procedure template (no return type
 
−
  declared). The execution continues from where the procedure
 
−
  was called.
 
  
−
---~---~---~---~---~---~---~---~---
+
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 <code>return;</code>. A function (<code>dilbegin integer name(...)</code>, <code>dilbegin string ...</code>) must use <code>return (value);</code>, and the call gives that value.
 +
 
 +
== Notes ==
 +
* vmc checks the form: <code>return;</code> in a function gives the error "no return expression expected" (meaning one is required), and <code>return (x);</code> 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. <code>return (null);</code> doesn't compile in a <code>unitptr</code> function; return a unit variable that is null instead.
 +
* In a program that wasn't called by another (one started by <code>dilcopy</code> or on a unit), <code>return</code> ends the program, as <code>quit</code> does.
 +
* Returning ends the template's <code>secure()</code>s and <code>interrupt()</code>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 <code>itoa()</code> take it directly.
 +
 
 +
== Example ==
 +
<pre>
 +
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
 +
</pre>
 +
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 ==
 +
* [[Manual:DIL Manual/return()|return()]]
 +
* [[Manual:DIL Manual/quit|quit]]
 +
* [[Manual:DIL Manual/dilbegin|dilbegin]]
 +
* [[Manual:DIL Manual/external|external]]

Latest revision as of 23:25, 14 November 2026

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