Basic Pascal Tutorial/Chapter 4/Forward Referencing: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 1 | + | [[Basic Pascal Tutorial/Chapter 4/Recursion|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 4/Programming Assignment|►]] | |
| 2 | + | ---|---|--- | |
| 3 | + | ||
| 4 | + | 4F - Forward Referencing (author: Tao Yue, state: unchanged) | |
| 4 | 5 | ||
| 5 | - | ||
| 6 | + | After all these confusing topics, here's something easy. | |
| 6 | 7 | ||
| 7 | - | ||
| 8 | + | Remember that procedures/functions can only see variables and other subprograms that have already been defined. Well, there is an exception. | |
| 8 | 9 | ||
| 9 | - | ||
| 10 | + | If you have two subprograms, each of which calls the other, you have a dilemma that no matter which you put first, the other still can't be called from the first. | |
| 10 | 11 | ||
| 11 | - | ||
| 12 | + | To resolve this chicken-and-the-egg problem, use _forward referencing_. | |
| 13 | + | ``` | |
| 12 | 14 | procedure Later (parameter list); forward; | |
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 15 | + | ||
| 16 | + | procedure Sooner (parameter list); | |
| 17 | + | begin | |
| 18 | + | ... | |
| 19 | + | Later (parameter list); | |
| 20 | + | end; | |
| 21 | + | ... | |
| 22 | + | procedure Later; | |
| 23 | + | begin | |
| 24 | + | ... | |
| 25 | + | Sooner (parameter list); | |
| 26 | + | end; | |
| 25 | 27 | ``` | |
| 26 | 28 | ||
| 27 | - | ||
| 29 | + | The same goes for functions. Just stick a forward; at the end of the heading. | |
| 28 | 30 | ||
| 31 | + | [[Basic Pascal Tutorial/Chapter 4/Recursion|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 4/Programming Assignment|►]] | |
| 32 | + | ---|---|--- | |
| 33 | + | ||
| 29 | 34 | --- | |
| 30 | - | ||
| 35 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Forward Referencing", as the Internet Archive kept it on 2025-04-01, last changed on 2025-03-02. 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).* |
