- 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 中,過程使用procedure關鍵字定義。過程定義的一般形式如下:
procedure name(argument(s): type1, argument(s): type 2, ... ); < local declarations > begin < procedure body > end;
Pascal 中的過程定義由頭部、區域性宣告和過程的主體組成。過程頭部由關鍵字procedure和給過程起的名字組成。以下是過程的所有部分:
引數- 引數建立了呼叫程式和過程識別符號之間的聯絡,也稱為形式引數。過程中的引數規則與函式的規則相同。
區域性宣告- 區域性宣告指的是標籤、常量、變數、函式和過程的宣告,這些宣告僅適用於過程的主體。
過程體- 過程體包含定義過程作用的一組語句。它應該始終包含在保留字begin和end之間。這是進行所有計算的過程部分。
以下是名為findMin()的過程的原始碼。此過程採用4個引數x、y、z和m,並將前三個變數中的最小值儲存在名為m的變數中。變數m透過引用傳遞(稍後我們將討論透過引用傳遞引數):
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 }
過程宣告
過程宣告告訴編譯器關於過程名稱以及如何呼叫過程的資訊。過程的實際主體可以單獨定義。
過程宣告具有以下語法:
procedure name(argument(s): type1, argument(s): type 2, ... );
請注意,過程的名稱不與任何型別關聯。對於上面定義的過程findMin(),宣告如下:
procedure findMin(x, y, z: integer; var m: integer);
呼叫過程
在建立過程時,您會給出過程必須執行的操作的定義。要使用該過程,您必須呼叫該過程來執行定義的任務。當程式呼叫過程時,程式控制將轉移到被呼叫過程。被呼叫的過程執行定義的任務,當到達其最後一個end語句時,它將控制返回給呼叫程式。
要呼叫過程,您只需將所需的引數與過程名稱一起傳遞,如下所示:
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
另一個例子是使用遞迴函式為給定數字生成斐波那契數列:
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
子程式的引數
如果子程式(函式或過程)要使用引數,它必須宣告接受引數值的變數。這些變數稱為子程式的形式引數。
形式引數在子程式內部的行為類似於其他區域性變數,並在進入子程式時建立,並在退出時銷燬。
呼叫子程式時,引數可以透過兩種方式傳遞給子程式:
| 序號 | 呼叫型別和描述 |
|---|---|
| 1 |
按值呼叫
此方法將引數的實際值複製到子程式的形式引數中。在這種情況下,對子程式內參數所做的更改不會影響引數。 |
| 2 |
按引用呼叫
此方法將引數的地址複製到形式引數中。在子程式內部,該地址用於訪問呼叫中使用的實際引數。這意味著對引數所做的更改會影響引數。 |
預設情況下,Pascal 使用按值呼叫傳遞引數。一般來說,這意味著子程式內的程式碼無法更改用於呼叫子程式的引數。“Pascal - 函式”一章中使用的示例程式使用按值呼叫呼叫名為max()的函式。
而此處提供的示例程式(exProcedure)使用按引用呼叫呼叫過程findMin()。