Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 4/Scope: what changed

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

OldNewLine
1-4D - Scope (author: Tao Yue, state: unchanged)
2-
3-*Scope* refers to where certain variables are visible. You have procedures inside procedures, variables inside procedures, and your job is to try to figure out when each variable can be seen by the procedure.
1+[[Basic Pascal Tutorial/Chapter 4/Functions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 4/Recursion|►]]
2+---|---|---
3+
4+4D - Scope (author: Tao Yue, state: unchanged)
45
5-A global variable is a variable defined in the main program. Any subprogram can see it, use it, and modify it. All subprograms can call themselves, and can call all other subprograms defined before it.
6+_Scope_ refers to where certain variables are visible. You have procedures inside procedures, variables inside procedures, and your job is to try to figure out when each variable can be seen by a procedure.
67
7-The main point here is: within any block of code (procedure, function, whatever), the only identifiers that are visible are those defined before that block and either in or outside of that block.
8+A global variable is a variable defined in the main program. Any subprogram can see it, use it, and modify it. All subprograms can call themselves, and can call all other subprograms defined before it.
89
9-``` pascal
10+The main point here is: within any block of code (procedure, function, whatever), the only identifiers that are visible are those defined before that block and either in or outside of that block.
11+```
1012program ScopeDemo;
11-var A : integer;
12-
13- procedure ScopeInner;
14- var A : integer;
15- begin
16- A := 10;
17- writeln (A)
18- end;
13+ var A : integer;
14+
15+ procedure ScopeInner;
16+ var A : integer;
17+ begin
18+ A := 10;
19+ writeln (A)
20+ end;
21+
22+ begin (* Main *)
23+ A := 20;
24+ writeln (A);
25+ ScopeInner;
26+ writeln (A);
27+ end. (* Main *)
28+```
1929
20-begin (* Main *)
21- A := 20;
22- writeln (A);
23- ScopeInner;
24- writeln (A);
25-end. (* Main *)
30+The output of the above program is:
2631```
32+20
33+ 10
34+ 20
35+```
2736
28-The output of the above program is:
37+The reason is: if two variables with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). The most local definition is used when one identifier is defined twice in different places.
2938
30-`20`\
31-`10`\
32-`20`
39+Here's a scope chart which basically amounts to an indented copy of a program with just the variables minus the logic. By stripping away the application logic and enclosing blocks in boxes, it is sometimes easier to see where certain variables are available.
3340
34-The reason is: if two variable with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). The most local definition is used when one identifier is defined twice in different places.
41+[![Scope.gif](/wiki-images/4/47/Scope.gif)](</File:Scope.gif>)
3542
36-Here's a scope chart which basically amounts to an indented copy of a program with just the variables and minus the logic. By stripping away the application logic and enclosing blocks in boxes, it is sometimes easier to see where certain variables are available. ![centre](/wiki-images/4/47/Scope.gif)
43+ * Everybody can see global variables `A, B`, and `C`.
44+ * However, in procedure `Alpha` the global definition of `A` is replaced by the local definition.
45+ * `Beta1` and `Beta2` can see variables `VCR, Betamax`, and `cassette`.
46+ * `Beta1` cannot see variable `FailureToo`, and `Beta2` cannot see `Failure`.
47+ * No subprogram except `Alpha` can access `F` and `G`.
48+ * Procedure `Beta` can call `Alpha` and `Beta`.
49+ * Function `Beta2` can call any subprogram, including itself (the main program is not a subprogram).
3750
38-- Everybody can see global variables `A, B`, and `C`.
39-- However, in procedure `Alpha` the global definition of `A` is replaced by the local definition.
40-- `Beta1` and `Beta2` can see variables `VCR, Betamax`, and `cassette`.
41-- `Beta1` cannot see variable `FailureToo`, and `Beta2` cannot see `Failure`.
42-- No subprogram except `Alpha` can access `F` and `G`.
43-- Procedure `Beta` can call `Alpha` and `Beta`.
44-- Function `Beta2` can call any subprogram, including itself (the main program is not a subprogram).
51+[[Basic Pascal Tutorial/Chapter 4/Functions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 4/Recursion|►]]
52+---|---|---
4553
4654---
47-*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Scope", 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).*
55+*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Scope", as the Internet Archive kept it on 2026-02-04, last changed on 2025-03-03. 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