目录

Pascal - 程序( Procedures)

Procedures是子程序,它不是返回单个值,而是允许获得一组结果。

定义程序

在Pascal中,使用procedure关键字定义procedure 。 程序定义的一般形式如下 -

procedure name(argument(s): type1, argument(s): type 2, ... );
   < local declarations >
begin
   < procedure body >
end;

Pascal中的过程definition由头,局部declarations和过程body组成。 过程标题由关键字procedure和给定procedure的名称组成。 以下是程序的所有部分 -

  • Arguments - 参数建立调用程序和过程标识符之间的链接,也称为形式参数。 过程中参数的规则与函数的参数规则相同。

  • Local declarations - 局部声明引用标签,常量,变量,函数和过程的声明,这些声明仅适用于过程主体。

  • Procedure Body - 过程主体包含一组语句,用于定义过程的作用。 它应始终包含在保留字开头和结尾之间。 它是完成所有计算的过程的一部分。

以下是名为findMin()的过程的源代码。 此过程采用4个参数x,y,z和m,并将前三个变量中的最小值存储在名为m的变量中。 变量m通过reference传递(稍后我们将通过引用讨论传递参数) -

procedure findMin(x, y, z: integer; var m: integer); 
(* Finds the minimum of the 3 values *)
begin
   if x < y then
      m := x
   else
      m := y;
   if z <m then
      m := z;
end; { end of procedure findMin }  

程序声明

过程declaration告诉编译器有关过程名称以及如何调用过程的信息。 程序的实际主体可以单独定义。

过程声明具有以下语法 -

procedure name(argument(s): type1, argument(s): type 2, ... );

请注意,该name of the procedure is not associated with any typename of the procedure is not associated with any type 。 对于上面定义的过程findMin() ,以下是声明 -

procedure findMin(x, y, z: integer; var m: integer);

调用程序

在创建过程时,您可以定义过程必须执行的操作。 要使用该过程,您必须调用该过程来执行定义的任务。 当程序调用过程时,程序控制将转移到被调用的过程。 被调用的过程执行定义的任务,当到达其最后一个结束语句时,它将控制返回给调用程序。

要调用过程,您只需要传递所需的参数以及过程名称,如下所示 -

program exProcedure;
var
   a, b, c,  min: integer;
procedure findMin(x, y, z: integer; var m: integer); 
(* Finds the minimum of the 3 values *)
begin
   if x < y then
      m:= x
   else
      m:= y;
   if z < m then
      m:= z;
end; { end of procedure findMin }  
begin
   writeln(' Enter three numbers: ');
   readln( a, b, c);
   findMin(a, b, c, min); (* Procedure call *)
   writeln(' Minimum: ', min);
end.

编译并执行上述代码时,会产生以下结果 -

Enter three numbers:
89 45 67
Minimum: 45

递归子程序

我们已经看到程序或子程序可能会调用另一个子程序。 当子程序调用自身时,它被称为递归调用,该过程称为递归。

为了说明这个概念,让我们计算一个数的阶乘。 数字n的因子定义为 -

n! = n*(n-1)!
   = n*(n-1)*(n-2)!
      ...
   = n*(n-1)*(n-2)*(n-3)... 1

以下程序通过递归调用自身来计算给定数字的阶乘。

program exRecursion;
var
   num, f: integer;
function fact(x: integer): integer; (* calculates factorial of x - x! *)
begin
   if x=0 then
      fact := 1
   else
      fact := x * fact(x-1); (* recursive call *)
end; { end of function fact}
begin
   writeln(' Enter a number: ');
   readln(num);
   f := fact(num);
   writeln(' Factorial ', num, ' is: ' , f);
end.

编译并执行上述代码时,会产生以下结果 -

Enter a number:
5
Factorial 5 is: 120

以下是另一个例子,它使用recursive函数为给定数字生成Fibonacci Series数列 -

program recursiveFibonacci;
var
   i: integer;
function fibonacci(n: integer): integer;
begin
   if n=1 then
      fibonacci := 0
   else if n=2 then
      fibonacci := 1
   else
      fibonacci := fibonacci(n-1) + fibonacci(n-2);
end; 
begin
   for i:= 1 to 10 do
   write(fibonacci (i), '  ');
end.

编译并执行上述代码时,会产生以下结果 -

0 1 1 2	3 5 8 13 21 34

子程序的论点

如果子程序( function or procedure )要使用参数,它必须声明接受参数值的变量。 这些变量称为子程序的formal parameters

形式参数的行为与子程序中的其他局部变量相似,并且在进入子程序时创建,并在退出时销毁。

在调用子程序时,有两种方法可以将参数传递给子程序 -

Sr.No 通话类型和说明
1 Call by value

此方法将参数的实际值复制到子程序的形式参数中。 在这种情况下,对子程序内的参数所做的更改对参数没有影响。

2 Call by reference

此方法将参数的地址复制到形式参数中。 在子程序内,该地址用于访问调用中使用的实际参数。 这意味着对参数所做的更改会影响参数。

默认情况下,Pascal使用call by value来传递参数。 通常,这意味着子程序中的代码不能改变用于调用子程序的参数。 我们在“Pascal - Functions”一章中使用的示例程序使用call by value名为max()的函数。

然而,此处提供的示例程序( exProcedure )使用call by reference调用过程findMin()。

↑回到顶部↑
WIKI教程 @2018