//格式
functionfunctionName(parameterList):returnType;directives;
localDeclarations;
begin
statements
end;
//例1
functionWF:Integer;
begin
WF:=17;
end;
//例2
functionWF:Integer;
begin
Result:=17;
end;
//例3function 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
例如:
functionMyFunction(X,Y:Real):Real;cdecl;
3.函數聲明
//先聲明後實現
functionCalculate(X,Y:Integer):Real;forward;
functionCalculate;
……{declarations}
begin
……{statementblock}
end;
//擴展聲明
//1
functionprintf(Format:PChar):Integer;cdecl;varargs;
//2LinkingtoObjectFiles
{$LBLOCK.OBJ}
procedureMoveWord(varSource,Dest;Count:Integer);external;
procedureFillWord(varDest;Data:Integer;Count:Integer);external;
//3ImportingFunctionsfromLibrarIEs
functionSomeFunction(S:string):string;external'strlib.dll';
//Thefollowingdeclarationimportsafunctionfromuser32.dll(partoftheWin32API).
functionMessageBox(HWnd:Integer;Text,Caption:PChar;Flags:Integer):Integer;stdcall;external'user32.dll'name'MessageBoxA';
4.函數重載
functionDivide(X,Y:Real):Real;overload;
begin
Result:=X/Y;
end
functionDivide(X,Y:Integer):Integer;overload;
begin
Result:=XdivY;
end;