Difference between revisions of "Manual:DIL Manual/return()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = return = | |
| − | + | '''Instruction.''' Leaves a procedure, or a function with its value. | |
| − | |||
| − | |||
| − | |||
| − | + | return; | |
| + | return(value); | ||
| − | + | <code>return;</code> leaves a procedure (a template without a return type) and goes on in the | |
| − | + | template that called it. <code>return(value);</code> leaves a function and gives | |
| − | + | <code>value</code> to the caller; the value must have the type the function was declared | |
| − | + | with (<code>dilbegin integer f(...)</code>). | |
| − | |||
| − | + | In the template a unit runs (the one copied onto it with <code>dilcopy</code>) there is no | |
| − | + | caller, and <code>return;</code> ends the program like [[Manual:DIL Manual/quit|quit]]. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | < | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == 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. | ||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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; | |
| − | is | + | code |
| − | + | { | |
| + | 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
Contents
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 aunitptrfunction:nullhas 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);withunull),vme.logsays "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.