- 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 使用按值呼叫方法來傳遞引數。通常,這意味著子程式中的程式碼不能改變用於呼叫子程式的引數。請考慮過程 swap() 的定義如下。
procedure swap(x, y: integer); var temp: integer; begin temp := x; x:= y; y := temp; end;
現在,讓我們透過傳遞實際值來呼叫過程 swap(),如下例所示 −
program exCallbyValue;
var
a, b : integer;
(*procedure definition *)
procedure swap(x, y: integer);
var
temp: integer;
begin
temp := x;
x:= y;
y := temp;
end;
begin
a := 100;
b := 200;
writeln('Before swap, value of a : ', a );
writeln('Before swap, value of b : ', b );
(* calling the procedure swap by value *)
swap(a, b);
writeln('After swap, value of a : ', a );
writeln('After swap, value of b : ', b );
end.
當編譯並執行以上程式碼時,它會產生以下結果 −
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :100 After swap, value of b :200
pascal_procedures.htm
廣告