Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 4/Scope/zh CN

Sent here from Scope/zh CN.

English · Français · Български · 中文(中国) · 日本語

4D - 变量作用域 (原作者: Tao Yue, 状态: 未更改)

变量作用域是指变量的使用范围。

全局变量,指在主程序中定义的变量。

它的作用范围:任何子程序可以使用、修改。

过程(函数)中定义的变量如果与全局变量同名,则它是局部变量,即仅在过程(函数)中有效。

这里的要点是:

text
代码块(过程、函数等等)中,变量作用域是,块内定义,块内可见。
pascal
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 *)

程序输出结果是:

text
20
10
20

原因是:

text
定义同名变量多次,如,在主程序、子程序都定义的相同名称的变量,它们调用时,都将调用自己的。

这是一个变量作用域图表,通过块缩进可以很清楚看到变量的使用范围。

上一页 目录 下一页

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.

Revision 4, last changed by WikiArchive .

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.

WhenWhoWhat changed
WangyouworldCreated page
Swen (minor)
Wangyouworld (minor)
FTurtle
TrevFixed syntax highlighting; deleted category included in page template (minor)
Kai BurghardtKai 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 Burghardtbypass redirects [cf. discussion]
Kai Burghardtfix previous edit (minor)
Kai Burghardtbypass 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.