//要點11: 參數可以分為: 默認參數(傳值)、var(傳址)、out(輸出)、const(常數)四 類
{默認參數是傳值, 不會被改變}
function MyF1(x: Integer): Integer;
begin
Inc(x);
Result := x;
end;
{var參數是傳址, 會被改變}
function MyF2(var x: Integer): Integer;
begin
Inc(x);
Result := x;
end;
{out參數是為支持Com的, 和 var 的結果是一樣的, 一般我們用不著它}
function MyF3(out x: Integer): Integer;
begin
Inc(x);
Result := x;
end;
{const參數是絕對不可以賦值的, 這是被編譯器優化的方式, 盡量多用}
function MyF4(const x: Integer): Integer;
begin
//Inc(x); {這句會出錯, 因為帶 const 前綴的參數是不可以更改的}
Result := x;
end;
//調用測試
procedure TForm1.Button1Click(Sender: TObject);
var
a: Integer;
begin
a := 6; MyF1(a);
ShowMessage(IntToStr(a)); //6
a := 6; MyF2(a);
ShowMessage(IntToStr(a)); //7
a := 6; MyF3(a);
ShowMessage(IntToStr(a)); //7
a := 6; MyF4(a);
ShowMessage(IntToStr(a)); //6
end;
//要點12: implementation 區中的過程或函數, 只能在本單元調用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{implementation 區中的過程或函數, 只能在本單元調用}
function MyFun(x,y: Integer): Integer;
begin
Result := x + y;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
i := MyFun(1,2);
end;
end.