在delphi過程、函式中傳遞引數幾個修飾符為const、var、out。
另一種不加修飾符的為預設按值傳遞引數。
一、預設方式以值方式傳遞引數procedure tform1.procnormal(value: string
); begin
orignum:=value+'me'
; lblreturn.caption:=orignum;//
orignum為'hello me'
lblorig.caption:=value;//
value為'hello'
end;
呼叫:
orignum:='
hello';
procnormal(orignum);
二、以const方式傳遞引數,這個引數在呼叫過程中不能改變,並且這種方式會被編譯器優化,一般建議儘可能地使用這種方式。
procedure tform1.procconst(const value: string
); begin
orignum:=value+'me'
; lblreturn.caption:=orignum;//
為'hello me‘
lblorig.caption:=value;//
為'hello me'
end;
三、按引用方式傳遞引數
procedure tform1.procref(var value: string
); begin
orignum:=value+'me'
; lblreturn.caption:=orignum;//
為'hello me‘
lblorig.caption:=value;//
為'hello me'
end;
四、按out方式傳遞引數,這個方式傳遞引數時,引數可以不被初始化,即使有值也被忽視,它一般用於輸出,它可以實現在一個過程中返回多個值,我們通常在分散式物件模型,如com中使用它。
procedure tform1.procout(out value: string
); begin
orignum:=value+'me'
; lblreturn.caption:=orignum;//
為'me'
lblorig.caption:=value;//
為'me'
end;
五、無型別引數,這是一種較為特殊的方法,引數的型別不確定,只能用const、var、out修飾,不能用於值方式傳遞引數,具體使用示例如下:
procedure tform1.procuntype(const
value);
begin
orignum:=string(value)+'me'
; lblreturn.caption:=orignum;//
為'hello me'
lblorig.caption:=string(value);//
為'hello me'
end;
六、預設引數,即如果此引數在呼叫時未提供時,將使用預設值。
procedure tform1.procdefault(const value, const defaultvalue:string='
123'
); begin
orignum:=value+'
me'+defaultvalue;
lblreturn.caption:=orignum;//
為'hello me 123'
lblorig.caption:=value;//
為'hello me 123'
end;
七、開放陣列引數,即引數陣列的元素個數不確定。
procedure tform1.procarray(const value: array
ofstring
); var
i:integer;
begin
for i:=low(value) to high(value) do
orignum:=orignum+value[i];//
呼叫後為'hello abc dbd'
lblreturn.caption:=orignum;
end;
呼叫:
orignum:='
hello';
procarray([
'abc
','dbd
']);
八、無型別開放陣列引數,即型別及元素個數皆不確定。在win32平臺中,這個引數的型別實際為array
oftvarrec,其使用示例如下:
procedure tform1.procarrayconst(const value: array
ofconst
); var
i:integer;
begin
for i:=low(value) to high(value) do
with value[i] do
case vtype of
vtansistring: orignum:= orignum+string(vansistring);
vtinteger: orignum:=orignum+inttostr(vinteger);
vtboolean: orignum := orignum +booltostr(vboolean);
vtchar: orignum := orignum +vchar;
vtextended: orignum := orignum +floattostr(vextended^);
vtstring: orignum := orignum +vstring^;
vtpchar: orignum := orignum +vpchar;
vtobject: orignum := orignum +vobject.classname;
vtclass: orignum := orignum +vclass.classname;
vtcurrency: orignum := orignum +currtostr(vcurrency^);
vtvariant: orignum := orignum + string
(vvariant^);
vtint64: orignum := orignum +inttostr(vint64^);
end;
lblreturn.caption:=orignum;//
呼叫後為'hello abc 3'
end;
呼叫:
orignum:='
hello';
procarrayconst([
'abc
',3]);
以上就是常見幾種傳遞引數的方式。