Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 4/Parameters/zh CN

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

4B - 参数传递 (原作者: Tao Yue, 状态: 未更改)

参数列表做为过程的一部分,允许传递参数给过程:

pascal
procedure ????????? (???????????????);

多个参数组间用分号分隔。

text
参数组1; 参数组2; ... ; 参数组n

参数组格式:

text
标识符1, 标识符2, ... , 标识符n : 数据类型

通过参数传递,以供过程使用这些参数,并且类型都与传递参数的相同。

pascal
procedure Name (a, b : integer; c, d : real);
begin
  a := 10;
  b := 2;
  writeln (a, b, c, d)
end;

传递参数给过程:

pascal
alpha := 30;
Name (alpha, 3, 4, 5);

在主程序中:

text
alpha值为30,之后,传递给过程中的a。

在过程中:

text
a重新赋值为10。实际发生的事情是,变量a和alpha值完全不同。

过程中的a赋值,并没有影响到主程序中的alpha变量。

这就是所谓的按值引用。

传递参数的另一种方式是按引用传递(按地址传递)。

按地址传递参数时,把实参变量的地址传送给被调用过程,形参和实参共用内存的同一地址。在被调用过程中,形参的值一旦改变,相应实参的值也跟着改变。

通过在参数组前加VAR关键字,实现按地址参数传递:

pascal
VAR ?????????1, ?????????2, ..., ?????????n : ????????????;

常量和文本不能做为实际参数,因为它们可能在过程中被改变。

下面是按值、按引用方式传递参数的示例:

pascal
procedure Name (a, b : integer; VAR c, d : integer);
begin
  c := 3;
  a := 5
end;

begin
  alpha := 1;
  gamma := 50;
  delta := 30;
  Name (alpha, 2, gamma, delta);
end.

程序运行后,gamma的值是3,因为c是按引用传递过来的参数,

alpha的值是1,因为它是按值传递过来的参数,看起来有点混乱。

按值传递参数,会复制一份传递过来的值,供过程使用。作用域只在过程中。

按地址传递参数,会使用同一变量值,相当于建立映射在同一内存地址。赋值操作会影响到外部对应变量。

按句话说: 按值传递参数,是单向的:主程序至过程。 按地址传递参数,是双向的。

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Parameters/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 3, last changed by WikiArchive .

Edited on wiki.freepascal.org by 4 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
FTurtle
TrevFixed syntax highlighting; removed categories included in template (minor)
Kai BurghardtKai Burghardt moved page Parameters/zh CN to Basic Pascal Tutorial/Chapter 4/Parameters/zh CN: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/... (minor)
Kai Burghardtbypass redirects [cf. discussion]
Kai Burghardtbypass [previously missed] redirect [cf. discussion]
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.