Difference between revisions of "Procedure"
(review; generalization (most content in Routine)) |
|||
Line 1: | Line 1: | ||
{{Procedure}} | {{Procedure}} | ||
− | + | A '''procedure''' is a [[Routine|routine]] that does not return a value. | |
− | A '''procedure''' is | + | <syntaxhighlight lang="pascal" enclose="none">procedure</syntaxhighlight> is a [[Reserved word|reserved word]]. |
− | + | == prematurely leaving a procedure == | |
+ | In a procedure the routine {{Doc|package=RTL|unit=system|identifier=exit|text=<syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight>}} can be called in order to (prematurely) leave the procedure. | ||
+ | <syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight> may not be supplied with any parameters, since procedures do not return any value, but [[Function|functions]] do. | ||
+ | Supplying a parameter to <syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight> inside a procedure definition will yield the compile-time error “Error: Procedures cannot return a value”. | ||
+ | == invocation == | ||
+ | Procedure calls are statements. | ||
+ | They may not appear in expressions, since they do not produce a value of any kind. | ||
+ | The following example highlights all lines with procedure calls. | ||
+ | <syntaxhighlight lang="pascal" line highlight="8,9,14,15"> | ||
+ | program procedureDemo(input, output, stderr); | ||
− | + | var | |
+ | x: longint; | ||
− | + | procedure foo; | |
− | + | begin | |
− | + | exit; | |
− | + | inc(x) | |
− | + | end; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | begin | ||
+ | x := 42; | ||
+ | foo; | ||
+ | writeLn(x); | ||
+ | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Note, that <syntaxhighlight lang="pascal" enclose="none">foo</syntaxhighlight> contains unreachable code (<syntaxhighlight lang="pascal" enclose="none">inc(x)</syntaxhighlight> is never executed because of the [unconditional] <syntaxhighlight lang="pascal" enclose="none">exit</syntaxhighlight>). | ||
− | == | + | == external references == |
− | + | * [https://www.freepascal.org/docs-html/ref/refsu53.html § “Procedure statements” in the Reference Guide] | |
− | + | * [https://www.freepascal.org/docs-html/ref/refse17.html § “Procedural types” in the Reference Guide] | |
− | |||
− | |||
− | |||
− | * [ | ||
− | |||
− | |||
− | |||
− | |||
− | * [ | ||
− | |||
− | |||
− |
Revision as of 13:49, 14 May 2018
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
italiano (it) │
русский (ru) │
A procedure is a routine that does not return a value.
procedure
is a reserved word.
prematurely leaving a procedure
In a procedure the routine exit
can be called in order to (prematurely) leave the procedure.
exit
may not be supplied with any parameters, since procedures do not return any value, but functions do.
Supplying a parameter to exit
inside a procedure definition will yield the compile-time error “Error: Procedures cannot return a value”.
invocation
Procedure calls are statements. They may not appear in expressions, since they do not produce a value of any kind. The following example highlights all lines with procedure calls.
1program procedureDemo(input, output, stderr);
2
3var
4 x: longint;
5
6procedure foo;
7begin
8 exit;
9 inc(x)
10end;
11
12begin
13 x := 42;
14 foo;
15 writeLn(x);
16end.
Note, that foo
contains unreachable code (inc(x)
is never executed because of the [unconditional] exit
).