以下列子 它们有什么分别,功用,效果和副作用?function Double(Value : integer) : Integer; <--这参数应该称什么?
begin
Double := Value*2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
x : Integer;
begin
x := Double(StrToInt(Edit1.Text));
Label1.Caption := IntToStr(x);
end;function vDouble(var Value : Integer) : Integer; <--引用参数
begin
vDouble := Value*2;
end;procedure TForm1.Button2Click(Sender: TObject);
var
x : Integer;
begin
x := StrToInt(Edit1.Text);
x := vDouble(x);
Label1.Caption := IntToStr(x);
Label2.Caption := 'This is no Different to Button3.';
end;function cDouble(const Value : Integer) : Integer; <--常量参数
begin
cDouble := Value*2;
end;procedure TForm1.Button4Click(Sender: TObject);
var
x : Integer;
begin
x := StrToInt(Edit1.Text);
x := cDouble(x);
Label1.Caption := IntToStr(x);
Label2.Caption := 'Similar like Button1';
end;
begin
Double := Value*2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
x : Integer;
begin
x := Double(StrToInt(Edit1.Text));
Label1.Caption := IntToStr(x);
end;function vDouble(var Value : Integer) : Integer; <--引用参数
begin
vDouble := Value*2;
end;procedure TForm1.Button2Click(Sender: TObject);
var
x : Integer;
begin
x := StrToInt(Edit1.Text);
x := vDouble(x);
Label1.Caption := IntToStr(x);
Label2.Caption := 'This is no Different to Button3.';
end;function cDouble(const Value : Integer) : Integer; <--常量参数
begin
cDouble := Value*2;
end;procedure TForm1.Button4Click(Sender: TObject);
var
x : Integer;
begin
x := StrToInt(Edit1.Text);
x := cDouble(x);
Label1.Caption := IntToStr(x);
Label2.Caption := 'Similar like Button1';
end;
解决方案 »
- 报错:F1026 file not found :'dxnavbar.dcu'
- 为什么我的代码不自动完成了?
- 如何用Indy中的idSMTPSever和idPop3Sever开发邮箱服务器程序
- Types of actual and formal var parameters must be identical这个错误指什么?
- @@##¥¥ --== 喜糖没有,喜分可以吧 ==-- ¥¥##@@
- 各位老大.给介绍一下.DELPHI哪个版本最好.
- 请问如何用ADOQuery实现动态查询?
- 如何将字符串中的汉字分别取出?急!!!
- 如何用程序屏蔽sqlserver 中的触发器?
- 如何建立一临时查询然后其中进行统计(不是生成表,因为如果是表的话有十几万条记录如何能快速删除?)如果使用视图,如何在不关闭连接的情况下动态生成视图呢?我使用的是interbase5.6数据库.(答对了我这有MM程序员的EMail哟!!!!!^Q^)
- ▲▲▲谁能帮我调下面的代码,各个按钮的功能?▲▲▲
- 请大家看下invalid floating point operation原因
第二个,可以修改参数的值
第三个,加上const就是希望参数不被修改
begin
Value := Value*2;
end;
function vDouble(var Value : Integer) : Integer; <--引用参数
begin
Value := Value*2;
end;function Double(Value : integer) : Integer; <--这参数应该称什么?
begin
Value := Value*2;
end;function cDouble(const Value : Integer) : Integer; <--常量参数
begin
Value := Value*2;
end;
这个正解,
带var 是引用,可以改变其值,方法体内与体外是一致的。
带const 是长量参数,不允许改变其值,在方法体内不能给这个参数设置值。
不带任何修饰的,在方法体内可以给其设置值,作用范围只能在方法体内,在方法体外仍然是传进来的值。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function Double_A(Value:integer):Integer;//不允许缺参数调用、参数能被改变
begin
Value:=Value+10;
Result:=Value;
end;function Double_B(var Value:integer):Integer;//不允许缺参数调用、参数必须是变量、参数能被改变
begin
Value:=Value+20;
Result:=Value;
end;function Double_C(const Value:integer=30):Integer;//允许不带参数调用(无参数调用时,使用30作传入值)、参数不能被改变
begin
//Value:=Value+10;//因为参数是常量,因此不能赋值给Value
Result:=Value+30;
end;function Double_D(const Value:integer):Integer;//不允许缺参数调用、参数不能被改变
begin
//Value:=Value+10;//因为参数是常量,因此不能赋值给Value
Result:=Value+40;
end;procedure TForm1.Button1Click(Sender: TObject);
var x:integer;
begin
x:=10; //Double_A 函数调用形式:
showmessage('Double_A函数(用常量作参数)传回的值是:'+inttostr(Double_A(10)));
showmessage('Double_A函数(用变量作参数)传回的值是:'+inttostr(Double_A(x)));
//showmessage('Double_A函数(无参数)传回的值是:'+inttostr(Double_A));//不能无参数 //Double_B 函数调用形式:
//showmessage('Double_B函数(用常量作参数)传回的值是:'+inttostr(Double_B(10)));//不能以常量作参数
showmessage('Double_B函数(用变量作参数)传回的值是:'+inttostr(Double_B(x)));
//showmessage('Double_B函数(无参数)传回的值是:'+inttostr(Double_B));//不能无参数 //Double_C 函数调用形式:
showmessage('Double_C函数(用常量作参数)传回的值是:'+inttostr(Double_C(10)));
showmessage('Double_C函数(用变量作参数)传回的值是:'+inttostr(Double_C(x)));
showmessage('Double_C函数(无参数)传回的值是:'+inttostr(Double_C)); //Double_D 函数调用形式:
showmessage('Double_D函数(用常量作参数)传回的值是:'+inttostr(Double_D(10)));
showmessage('Double_D函数(用变量作参数)传回的值是:'+inttostr(Double_D(x)));
//showmessage('Double_D函数(无参数)传回的值是:'+inttostr(Double_D));//不能无参数
end;end.
ss:='A';
x(ss);
1:无修饰,s值变成了B,但ss还是A
procedure x(s:string);
begin
s:='B';
end;2.var修饰,s值变成了B,ss也变成B
procedure x(var s:string);
begin
s:='B';
end;3.Const修饰,s是传入什么,就是什么,不能改变
procedure x(Const s:string);
begin
//s:='B'; 这里不能再重新赋值
end;