Basic Pascal Tutorial/Chapter 4/Scope/zh CN
Sent here from Scope/zh CN.
English · Français · Български · 中文(中国) · 日本語
4D - 变量作用域 (原作者: Tao Yue, 状态: 未更改)
变量作用域是指变量的使用范围。
全局变量,指在主程序中定义的变量。
它的作用范围:任何子程序可以使用、修改。
过程(函数)中定义的变量如果与全局变量同名,则它是局部变量,即仅在过程(函数)中有效。
这里的要点是:
代码块(过程、函数等等)中,变量作用域是,块内定义,块内可见。program ScopeDemo;
var A : integer;
procedure ScopeInner;
var A : integer;
begin
A := 10;
writeln (A)
end;
begin (* Main *)
A := 20;
writeln (A);
ScopeInner;
writeln (A);
end. (* Main *)程序输出结果是:
20
10
20原因是:
定义同名变量多次,如,在主程序、子程序都定义的相同名称的变量,它们调用时,都将调用自己的。这是一个变量作用域图表,通过块缩进可以很清楚看到变量的使用范围。

- 所有地方都可以使用全局变量
A, B, C。 - 然而, 在Alpha过程中新定义了变量A,它将取代全局变量A。
- 过程Beta中定义的变量
VCR, Betamax, cassette,在过程Beta1、函数Beta2都可以使用。 - 过程Beta1不能使用函数Beta2定义的变量FailureToo;同理,函数Beta2也不能使用Beta1过程的Failure变量。
- 除了Alpha过程可以使用F, G变量,其他地方都不能。
- 过程Beta可以调用过程Alpha和Beta。
- 函数Beta2可以调用任何子程序,包括自身(主程序不是一个子程序)
| 上一页 | 目录 | 下一页 |
From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Scope/zh CN", 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.
Edited on wiki.freepascal.org by 5 people, 2013–2022
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Wangyouworld | Created page | |
| Swen | (minor) | |
| Wangyouworld | (minor) | |
| FTurtle | ||
| Trev | Fixed syntax highlighting; deleted category included in page template (minor) | |
| Kai Burghardt | Kai Burghardt moved page Scope/zh CN to Basic Pascal Tutorial/Chapter 4/Scope/zh CN: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Clu... (minor) | |
| Kai Burghardt | bypass redirects [cf. discussion] | |
| Kai Burghardt | fix previous edit (minor) | |
| Kai Burghardt | bypass language bar/categorization template redirect [cf. discussion] (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
