Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 4/Procedures: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Basic Pascal Tutorial/Chapter 4/Procedures

OldNewLine
1+4A - Procedures (author: Tao Yue, state: unchanged)
2+
3+A procedure is a subprogram. Subprograms help reduce the amount of redundancy in a program. Statements that are executed over and over again but not contained in a loop are often put into subprograms.
4+
5+Subprograms also facilitate top-down design. Top-down design is the tackling of a program from the most general to the most specific. For example, top down design for going from one room to another starts out as:
6+
7+- Get out of first room
8+- Go to second room
9+- Go into second room
10+
11+Then it is refined to
12+
13+- Get out of first room
14+ - Go to door
15+ - Open the door
16+ - Get out of door
17+ - Close door
18+- ...
19+
20+Just going to the door can be refined further:
21+
22+- Get out of first room
23+ - Go to door
24+ - Get out of seat
25+ - Turn towards door
26+ - Walk until you almost bump into it
27+
28+This, of course, can be further refined to say how much exercise should be given to your cardiac myofibrils, and how much adenosine diphosphate should be converted to adenosine triphosphate by fermentation or aerobic respiration. This may seem to be too detailed, but for computer programming, this is, in effect what you have to do. The computer can't understand general statements -- you must be specific.
29+
30+Main tasks should be contained in procedures, so in the main program, you don't have to worry about the details. This also makes for reusable code. You can just keep your procedures in one file and link that into your program.
31+
32+A procedure has the same basic format as a program:
33+
34+``` pascal
35+procedure Name;
36+
37+const
38+ (* Constants *)
39+
40+var
41+ (* Variables *)
42+
43+begin
44+ (* Statements *)
45+end;
46+```
47+
48+There is a semicolon (not a period) at the end.
49+
50+To call the procedure from the main program, just use the name, like you would `writeln`.
51+
52+``` pascal
53+ Name;
54+```
55+
56+Procedures are very often used to output data. It's that simple (until the next lesson, of course).
57+
58+---
59+*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Procedures", as it stood on 2022-08-20 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).*

Back to history