- Pascal 教程
- Pascal - 首页
- Pascal - 概述
- Pascal - 环境设置
- Pascal - 程序结构
- Pascal - 基本语法
- Pascal - 数据类型
- Pascal - 变量类型
- Pascal - 常量
- Pascal - 运算符
- Pascal - 决策制定
- Pascal - 循环
- Pascal - 函数
- Pascal - 过程
- Pascal - 变量作用域
- Pascal - 字符串
- Pascal - 布尔值
- Pascal - 数组
- Pascal - 指针
- Pascal - 记录
- Pascal - 变体
- Pascal - 集合
- Pascal - 文件处理
- Pascal - 内存
- Pascal - 单元
- Pascal - 日期和时间
- Pascal - 对象
- Pascal - 类
- Pascal 有用资源
- Pascal - 快速指南
- Pascal - 有用资源
- Pascal - 讨论
Pascal - 变量作用域
在任何编程语言中,作用域是指程序中一个定义的变量可以存在并且可以访问的区域,超出该区域则无法访问该变量。在 Pascal 编程语言中,可以在三个地方声明变量:
在子程序或块内,称为局部变量
在所有子程序之外,称为全局变量
在子程序参数的定义中,称为形式参数
让我们解释一下什么是局部变量和全局变量以及形式参数。
局部变量
在子程序或代码块内部声明的变量称为局部变量。只有在该子程序或代码块内部的语句才能使用它们。局部变量对外部的子程序是未知的。以下是用局部变量的示例。这里,所有变量a、b和c都是名为exLocal的程序的局部变量。
program exLocal;
var
a, b, c: integer;
begin
(* actual initialization *)
a := 10;
b := 20;
c := a + b;
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end.
当以上代码被编译和执行时,会产生以下结果:
value of a = 10 b = 20 c = 30
现在,让我们稍微扩展一下程序,创建一个名为 display 的过程,该过程将拥有自己的变量集a、b和c,并从程序exLocal中显示它们的值。
program exLocal;
var
a, b, c: integer;
procedure display;
var
a, b, c: integer;
begin
(* local variables *)
a := 10;
b := 20;
c := a + b;
writeln('Winthin the procedure display');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end;
begin
a:= 100;
b:= 200;
c:= a + b;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当以上代码被编译和执行时,会产生以下结果:
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display value of a = 10 b = 20 c = 30
全局变量
全局变量是在函数外部定义的,通常位于程序的顶部。全局变量将在程序的整个生命周期内保持其值,并且可以在为程序定义的任何函数内部访问它们。
任何函数都可以访问全局变量。也就是说,全局变量在其声明后即可在整个程序中使用。以下是用全局和局部变量的示例:
program exGlobal;
var
a, b, c: integer;
procedure display;
var
x, y, z: integer;
begin
(* local variables *)
x := 10;
y := 20;
z := x + y;
(*global variables *)
a := 30;
b:= 40;
c:= a + b;
writeln('Winthin the procedure display');
writeln(' Displaying the global variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
writeln('Displaying the local variables x, y, and z');
writeln('value of x = ', x , ' y = ', y, ' and z = ', z);
end;
begin
a:= 100;
b:= 200;
c:= 300;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当以上代码被编译和执行时,会产生以下结果:
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display Displaying the global variables a, b, and c value of a = 30 b = 40 c = 70 Displaying the local variables x, y, and z value of x = 10 y = 20 z = 30
请注意,过程 display 可以访问变量 a、b 和 c,它们相对于 display 以及它自己的局部变量来说是全局变量。程序可以为局部变量和全局变量使用相同的名称,但函数内部局部变量的值将优先。
让我们稍微修改一下前面的例子,现在过程 display 的局部变量与a、b、c具有相同的名称:
program exGlobal;
var
a, b, c: integer;
procedure display;
var
a, b, c: integer;
begin
(* local variables *)
a := 10;
b := 20;
c := a + b;
writeln('Winthin the procedure display');
writeln(' Displaying the global variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
writeln('Displaying the local variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end;
begin
a:= 100;
b:= 200;
c:= 300;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当以上代码被编译和执行时,会产生以下结果:
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display Displaying the global variables a, b, and c value of a = 10 b = 20 c = 30 Displaying the local variables a, b, and c value of a = 10 b = 20 c = 30
广告