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="fret2"></span>
+
= return =
−
return():
+
'''Instruction.''' Leaves a procedure, or a function with its value.
−
  Return from a call to a function (return type declared).
 
−
  The expression inside the parenthesis evaluates to the value
 
−
  returned.
 
  
−
  ---~---~---~---~---~---~---~---~---
+
  return;
 +
return(value);
  
−
  DIL also allows for game-time *symbolic* resolving of
+
<code>return;</code> leaves a procedure (a template without a return type) and goes on in the
−
  function/procedure names. This allows you to pass template names
+
template that called it. <code>return(value);</code> leaves a function and gives
−
  as string arguments and call them in your program. This kind of
+
<code>value</code> to the caller; the value must have the type the function was declared
−
  function call requires taxing look ups and type check runtime, so
+
with (<code>dilbegin integer f(...)</code>).
−
  use the normal kind of procedures if possible.
 
  
−
  In effect, the syntax of a symbolic call is similar to that of
+
In the template a unit runs (the one copied onto it with <code>dilcopy</code>) there is no
−
  a normal, except you use a string variable as function name
+
caller, and <code>return;</code> ends the program like [[Manual:DIL Manual/quit|quit]].
−
  instead of the real function name. If the string variable contains
 
−
  a valid reference to a procedure/function, and all the argument
 
−
  types matches the found templates argument types, the call
 
−
  is made. Otherwise, the call is skipped. Note that you will get no
 
−
  type check by the compiler when using symbolic references.
 
−
<span id="fret2"></span>
 
−
return():
 
−
  Return from a call to a function (return type declared).
 
−
  The expression inside the parenthesis evaluates to the value
 
−
  returned.
 
  
−
---~---~---~---~---~---~---~---~---
+
== Notes ==
 +
* vmc checks the form: <code>return;</code> in a function gives "no return expression expected", <code>return(x);</code> in a procedure, or with a value of the wrong type, "return expression not of correct type".
 +
* <code>return(null);</code> doesn't compile in a <code>unitptr</code> function: <code>null</code> has a type of its own. Return a unit variable that is null instead (see the example on [[Manual:DIL Manual/while()|while()]]).
 +
* If the value fails at run time (a field of a null unit, e.g. <code>return(u.level);</code> with <code>u</code> null), <code>vme.log</code> says "DIL: Error return types do not match." and the whole program stops. Test for null first.
 +
* [[Manual:DIL Manual/quit|quit]] in a procedure or function doesn't return: it ends the whole program, callers too.
 +
* Secures made in the template end when it returns.
 +
* A string or a list is copied for the caller.
  
−
  DIL also allows for game-time *symbolic* resolving of
+
== Example ==
−
   function/procedure names. This allows you to pass template names
+
<pre>
−
   as string arguments and call them in your program. This kind of
+
dilbegin integer is_mortal(u : unitptr);
−
  function call requires taxing look ups and type check runtime, so
+
code
−
  use the normal kind of procedures if possible.
+
{
 +
   if (u.type != UNIT_ST_PC)
 +
      return(FALSE);
 +
   return(u.level < IMMORTAL_LEVEL);
 +
}
 +
dilend
  
−
   In effect, the syntax of a symbolic call is similar to that of
+
dilbegin greet_mortal(u : unitptr);
−
   a normal, except you use a string variable as function name
+
external
−
   instead of the real function name. If the string variable contains
+
   integer is_mortal(u : unitptr);
−
   a valid reference to a procedure/function, and all the argument
+
var
−
   types matches the found templates argument types, the call
+
   i : integer;
−
   is made. Otherwise, the call is skipped. Note that you will get no
+
code
−
  type check by the compiler when using symbolic references.
+
{
 +
   i := is_mortal(u);
 +
   if (i == FALSE)
 +
      return;                    // back to the caller
 +
   exec("bow " + u.name, self);
 +
   return;
 +
}
 +
dilend
 +
</pre>
 +
A function that tells whether a unit is a mortal player, and a procedure that leaves early when it isn't.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/quit|quit]]
 +
* [[Manual:DIL Manual/dilbegin|dilbegin]]
 +
* [[Manual:DIL Manual/external|external]]
 +
* [[Manual:DIL Manual/dilcall()|dilcall()]]

Latest revision as of 23:22, 14 November 2026

return

Instruction. Leaves a procedure, or a function with its value.

return;
return(value);

return; leaves a procedure (a template without a return type) and goes on in the template that called it. return(value); leaves a function and gives value to the caller; the value must have the type the function was declared with (dilbegin integer f(...)).

In the template a unit runs (the one copied onto it with dilcopy) there is no caller, and return; ends the program like quit.

Notes

  • vmc checks the form: return; in a function gives "no return expression expected", return(x); in a procedure, or with a value of the wrong type, "return expression not of correct type".
  • return(null); doesn't compile in a unitptr function: null has a type of its own. Return a unit variable that is null instead (see the example on while()).
  • If the value fails at run time (a field of a null unit, e.g. return(u.level); with u null), vme.log says "DIL: Error return types do not match." and the whole program stops. Test for null first.
  • quit in a procedure or function doesn't return: it ends the whole program, callers too.
  • Secures made in the template end when it returns.
  • A string or a list is copied for the caller.

Example

dilbegin integer is_mortal(u : unitptr);
code
{
   if (u.type != UNIT_ST_PC)
      return(FALSE);
   return(u.level < IMMORTAL_LEVEL);
}
dilend

dilbegin greet_mortal(u : unitptr);
external
   integer is_mortal(u : unitptr);
var
   i : integer;
code
{
   i := is_mortal(u);
   if (i == FALSE)
      return;                    // back to the caller
   exec("bow " + u.name, self);
   return;
}
dilend

A function that tells whether a unit is a mortal player, and a procedure that leaves early when it isn't.

See also