Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə123/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   119   120   121   122   123   124   125   126   ...   294
DelphiLanguageGuide

Procedural Constants
To declare a procedural constant, specify the name of a function or procedure that is compatible with the declared
type of the constant. For example,
function Calc(X, Y: Integer): Integer;
begin
  ...
end;
 
type TFunction = function(X, Y: Integer): Integer;
const MyFunction: TFunction = Calc;
Given these declarations, you can use the procedural constant 
MyFunction
 in a function call:
I := MyFunction(5, 7)
You can also assign the value nil to a procedural constant.
Pointer Constants
When you declare a pointer constant, you must initialize it to a value that can be resolvedat least as a relative
addressat compile time. There are three ways to do this: with the @ operator, with nil, and (if the constant is of type
107


PChar or PWideChar) with a string literal. For example, if 
I
 is a global variable of type Integer, you can declare a
constant like
const PI: ^Integer = @I;
The compiler can resolve this because global variables are part of the code segment. So are functions and global
constants:
const PF: Pointer = @MyFunction;
Because string literals are allocated as global constants, you can initialize a PChar constant with a string literal:
const WarningStr: PChar = 'Warning!';
108


Procedures and Functions
This section describes the syntax of function and procedure declarations.
109


Procedures and Functions
This topic covers the following items:
Declaring procedures and functions
Calling conventions
Forward and interface declarations
Declaration of external routines
Overloading procedures and functions
Local declarations and nested routines
About Procedures and Functions
Procedures and functions, referred to collectively as routines, are self-contained statement blocks that can be called
from different locations in a program. A function is a routine that returns a value when it executes. A procedure is a
routine that does not return a value.
Function calls, because they return a value, can be used as expressions in assignments and operations. For
example,
I := SomeFunction(X);
calls 
SomeFunction
 and assigns the result to 
I
. Function calls cannot appear on the left side of an assignment
statement.
Procedure calls - and, when extended syntax is enabled (
{$X+}
), function calls - can be used as complete
statements. For example,
DoSomething;
calls the 
DoSomething
 routine; if 
DoSomething
 is a function, its return value is discarded.
Procedures and functions can call themselves recursively.
Declaring Procedures and Functions
When you declare a procedure or function, you specify its name, the number and type of parameters it takes, and,
in the case of a function, the type of its return value; this part of the declaration is sometimes called the prototype,
heading, or header. Then you write a block of code that executes whenever the procedure or function is called; this
Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   119   120   121   122   123   124   125   126   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin