程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi語言學習5-函數和方法

Delphi語言學習5-函數和方法

編輯:Delphi

1.函數的定義

//格式
function functionName(parameterList): returnType; directives;
localDeclarations;
begin
statements
end;
//例1
function WF: Integer;
begin
WF := 17;
end;
//例2
function WF: Integer;
begin
Result := 17;
end;
//例3 function MyFunction: Integer;
begin
MyFunction := 5;
Result := Result * 2;
MyFunction := Result + 1;
end;

2.調用方式

Delphi的函數有幾種調用方式,

Directive Parameter order Clean-up Passes parameters in registers? register Left-to-right Routine Yes pascal Left-to-right Routine No cdecl Right-to-left Caller No stdcall Right-to-left Routine No safecall Right-to-left Routine No

例如:

function MyFunction(X, Y: Real): Real; cdecl;

3.函數聲明

//先聲明後實現
function Calculate(X, Y: Integer): Real; forward;
function Calculate;
... { declarations }
begin
... { statement block }
end;
//擴展聲明
//1
function printf(Format: PChar): Integer; cdecl; varargs;
//2 Linking to Object Files
{$L BLOCK.OBJ}
procedure MoveWord(var Source, Dest; Count: Integer); external;
procedure FillWord(var Dest; Data: Integer; Count: Integer); external;
//3 Importing Functions from Libraries
function SomeFunction(S: string): string; external 'strlib.dll';
//The following declaration imports a function from user32.dll (part of the Win32 API).
function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';

4.函數重載

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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved