Procedure: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Procedure
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **procedure** is a [[Routine|routine]] that does not return a value. `procedure` is a [[Reserved word|reserved word]]. | |
| 2 | + | ||
| 3 | + | ## Prematurely leaving a procedure | |
| 4 | + | ||
| 5 | + | In a procedure the routine [`exit`](https://www.freepascal.org/docs-html/rtl/system/exit.html) 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 [[Function|functions]] do. Supplying a parameter to `exit` inside a procedure definition will yield the compile-time error “Error: Procedures cannot return a value”. | |
| 6 | + | ||
| 7 | + | ## Invocation | |
| 8 | + | ||
| 9 | + | 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. | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | program procedureDemo(input, output, stderr); | |
| 13 | + | ||
| 14 | + | var | |
| 15 | + | x: longint; | |
| 16 | + | ||
| 17 | + | procedure foo; | |
| 18 | + | begin | |
| 19 | + | exit; | |
| 20 | + | inc(x); | |
| 21 | + | end; | |
| 22 | + | ||
| 23 | + | begin | |
| 24 | + | x := 42; | |
| 25 | + | foo; | |
| 26 | + | writeLn(x); | |
| 27 | + | end. | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | Note, that `foo` contains unreachable code (`inc(x)` is never executed because of the \[unconditional\] `exit`). | |
| 31 | + | ||
| 32 | + | ## See also | |
| 33 | + | ||
| 34 | + | - [[Basic Pascal Tutorial/Chapter 4/Procedures|Tutorial: procedures]] | |
| 35 | + | - [§ “Procedure statements” in the Reference Guide](https://www.freepascal.org/docs-html/ref/refsu53.html) | |
| 36 | + | - [§ “Procedural types” in the Reference Guide](https://www.freepascal.org/docs-html/ref/refse17.html) | |
| 37 | + | ||
| 38 | + | --- | |
| 39 | + | *From the Free Pascal wiki, page "Procedure", as it stood on 2023-01-25 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
