1. 作为变量定义 procedure TForm1.xxxx(Sender: TObject) var X, Y, Z: Double; I, J, K: Integer; Digit: 0..9; Okay: Boolean; begin ..... end;2. 作为参数的传址方式调用 我们学C语言的时候,知道如下函数并不能交换实参的数值 procedure swap(X, Y: Integer); var Tmp: Integer; begin Tmp:=X; X:=Y; Y:=Tmp; end;当调用swap(a,b)后,a,b的值并没有交换;在C语言中可以采用指针传址解决;在Delphi中,可以如下解决: procedure swap(var X, Y: Integer); var Tmp: Integer; begin Tmp:=X; X:=Y; Y:=Tmp; end;
to DaSaint(齐天大圣)你所说的是 function和procedure定义形参时,加var和不加的区别,俺能理解?var( );这个在delphi中是一个单独的函数,可以判断是否为数值型的.但里面参数我不解,help me! 谢谢各位,谢谢
... 这不是对函数的修饰 是对参数的修饰。就是一个 pass by value vs pass by reference呀楼主 你在哪儿见过的?? 贴出来看看。
你记错了,是Val,查查帮助吧 var i: Integer; { 从Edit1得到数值 } Val(Edit1.Text, I, Code); { 是整数么 } if Code <> 0 then MessageDlg('在字符串第: ' + IntToStr(Code) + '错误', mtWarning, [mbOk], 0); else Label1.Caption := IntToStr(I)); code是发生错误的位数,如果是0就是正确的。
看了你的答案,俺开了翘 to asj(最烦活稀泥) 谢谢
var函数是在哪一个单元,又怎么查询一个函数所在的单元?
val... Converts a string to a numeric representation.UnitSystemCategorystring handling routinesDelphi syntax:procedure Val(S; var V; var Code: Integer);DescriptionIn Delphi code, Val converts the string value S to its numeric representation, as if it were read from a text file with Read.S is a string-type expression; it must be a sequence of characters that form a signed real number. V is an integer-type or real-type variable. If V is an integer-type variable, S must form a whole number. Code is a variable of type Integer.If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.Val performs range checking differently depending upon the setting of the $R compiler directive and the type of the parameter V.Setting Result{$R+} An out-of-range value always generates a runtime error. {$R-} The values for out-of-range vary depending upon the data type of V.// 打开delphi帮助文件 然后在索引处输入 val....
procedure TForm1.xxxx(Sender: TObject)
var
X, Y, Z: Double;
I, J, K: Integer;
Digit: 0..9;
Okay: Boolean;
begin
.....
end;2. 作为参数的传址方式调用
我们学C语言的时候,知道如下函数并不能交换实参的数值
procedure swap(X, Y: Integer);
var Tmp: Integer;
begin
Tmp:=X;
X:=Y;
Y:=Tmp;
end;当调用swap(a,b)后,a,b的值并没有交换;在C语言中可以采用指针传址解决;在Delphi中,可以如下解决:
procedure swap(var X, Y: Integer);
var Tmp: Integer;
begin
Tmp:=X;
X:=Y;
Y:=Tmp;
end;
这不是对函数的修饰 是对参数的修饰。就是一个 pass by value vs pass by reference呀楼主 你在哪儿见过的?? 贴出来看看。
var
i: Integer;
{ 从Edit1得到数值 }
Val(Edit1.Text, I, Code);
{ 是整数么 }
if Code <> 0 then
MessageDlg('在字符串第: ' + IntToStr(Code) + '错误', mtWarning, [mbOk], 0);
else
Label1.Caption := IntToStr(I));
code是发生错误的位数,如果是0就是正确的。
Converts a string to a numeric representation.UnitSystemCategorystring handling routinesDelphi syntax:procedure Val(S; var V; var Code: Integer);DescriptionIn Delphi code, Val converts the string value S to its numeric representation, as if it were read from a text file with Read.S is a string-type expression; it must be a sequence of characters that form a signed real number. V is an integer-type or real-type variable. If V is an integer-type variable, S must form a whole number. Code is a variable of type Integer.If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.Val performs range checking differently depending upon the setting of the $R compiler directive and the type of the parameter V.Setting Result{$R+} An out-of-range value always generates a runtime error.
{$R-} The values for out-of-range vary depending upon the data type of V.// 打开delphi帮助文件 然后在索引处输入 val....