首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 函數與方法全解析:定義、調用約定、前向聲明、外部連結與重載

     編輯:Delphi

Delphi 函數是程式碼複用的基本單元。本文涵蓋函數定義語法、返回值處理、調用約定、前向聲明、外部連結(DLL/Obj)、函數重載等核心知識,並對比方法與獨立函數的異同,提供完整可編譯範例。

1. 函數定義基本結構

function FunctionName(ParameterList): ReturnType; Directives;
  LocalDeclarations;
begin
  Statements;
end;

三種返回值寫法

{ 寫法 1:賦值給函數名 }
function WF: Integer;
begin
  WF := 17;
end;

{ 寫法 2:使用 Result 隱含變數(推薦) }
function WF: Integer;
begin
  Result := 17;
end;

{ 寫法 3:混合使用(Result 可多次賦值) }
function MyFunction: Integer;
begin
  MyFunction := 5;
  Result := Result * 2;   // Result = 10
  MyFunction := Result + 1; // 返回 11
end;

建議:統一使用 Result,可讀性最好,且支援提前退出(Exit(Result))。

2. 調用約定

約定參數壓棧順序棧清理者寄存器傳參典型用途
register左→右被調用者是(前 3 個)Delphi 預設,效能最佳
pascal左→右被調用者否舊版 Pascal 相容
cdecl右→左調用者否C/C++、可變參數
stdcall右→左被調用者否Win32 API、COM
safecall右→左被調用者否COM、自動 HRESULT 轉異常
// 範例
function MyFunction(X, Y: Real): Real; cdecl;
function WinAPICall(Param: Integer): Integer; stdcall; external 'user32.dll' name 'RealFunctionName';

3. 前向聲明

function Calculate(X, Y: Integer): Real; forward;  // 僅聲明

// ... 其他代碼 ...

function Calculate;  // 實現時可省略參數列表
  // 區域聲明
begin
  // 實現
end;
  • 解決相互遞歸呼叫、單元間循環依賴。
  • 實現處參數列表可省略,但返回類型與指令必須一致。

4. 外部連結

連結目標檔

{$L BLOCK.OBJ}
procedure MoveWord(var Source, Dest; Count: Integer); external;
procedure FillWord(var Dest; Data: Integer; Count: Integer); external;

從 DLL 匯入

function SomeFunction(S: string): string; external 'strlib.dll';

function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer;
  stdcall; external 'user32.dll' name 'MessageBoxA';  // 指定實際導出名
  • external 'dllname':延遲載入 DLL,首次呼叫時載入。
  • name 'exportname':導出名與 Delphi 名稱不同時使用。
  • 字串參數跨 DLL 邊界建議用 PChar 或 WideString(BSTR),避免記憶體管理器不一致導致崩潰。

5. 函數重載

function Divide(X, Y: Real): Real; overload;
begin
  Result := X / Y;
end;

function Divide(X, Y: Integer): Integer; overload;
begin
  Result := X div Y;
end;

// 呼叫
var
  R1: Real; I1: Integer;
begin
  R1 := Divide(10.0, 3.0);  // 3.333...
  I1 := Divide(10, 3);      // 3
end;

重載規則

  • 參數列表必須不同(類型、數量、順序至少一項差異)。
  • 返回類型不可作為區分依據。
  • 預設參數與重載結合時需謹慎,避免歧義。

6. 方法 vs 獨立函數

7. 進階技巧

內聯函數

function FastAdd(A, B: Integer): Integer; inline;
begin
  Result := A + B;
end;

編譯器會在呼叫點展開代碼,適合極小、高頻函數。

泛型函數

function Max(const A, B: T): T;
begin
  if A > B then Result := A else Result := B;
end;

匿名方法

type
  TIntFunc = reference to function(X: Integer): Integer;

procedure UseFunc(F: TIntFunc);
begin
  WriteLn(F(10));
end;

begin
  UseFunc(function(X: Integer): Integer begin Result := X * 2 end);
end;

常見錯誤與調試技巧

  • 忘記返回值:啟用編譯器警告 WARN_RETURN_VALUE 或靜態分析工具。
  • 棧平衡錯誤:混用調用約定(特別是 cdecl/stdcall)導致棧損壞,檢查 external 聲明是否與 DLL 導出一致。
  • 重載歧義:F(1) 無法決定呼叫 F(Integer) 或 F(Int64),需強制轉型 F(Integer(1))。

掌握函數定義、調用約定、外部互操作與重載機制,能讓 Delphi 程式在效能、相容性與可維護性之間取得最佳平衡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
特性獨立函數方法
聲明位置單元介面/實現區類型定義內部
隱含參數無Self(實例/類別參考)
存取權限單元可見性private/protected/public/published
多態不支援支援 virtual/override
重載支援支援