函数的可选参数 ( 积分: 50 )

  • 主题发起人 主题发起人 slyp
  • 开始时间 开始时间
S

slyp

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中,怎么定义一个函数带可选参数的?
上网找了个格式居然是错的
请各位试下再发上来,免得有问题找不到人
 
在DELPHI中,怎么定义一个函数带可选参数的?
上网找了个格式居然是错的
请各位试下再发上来,免得有问题找不到人
 
没有明白你得到什么样的效果?是不是传给函数的某几个参数可填可不填是吗?
 
给参数设缺省值或用overload
 
procedure FillArray(A: array of Integer
Value: Integer = 0);
Value为可选参数.
 
看源代码!随便找个函数带可选参数的源代码一看定义就知道了。
 
是可填可不填,但不是默认值
 
procedure Test(Params: array of const);
是指这个?
 
用overload,这样调用时参数就可有可无了
procedure MyProc(param: int)
overload;
procedure MyProc
overload;
 
同意用overload
 
delphi的object pascal不支持可选参数;但是free pascal联盟提供的编译器支持可选参数
 
函数重载就可以。overload,楼上几位已经说了
 
xeen的是正确的,可选参数一般放在最后
 
procedure FillArray(A: array of Integer
Value: Integer = 0);Value为可选参数.
可选参数必须放在参数列表的最后,它的类型必须为简单类型。
 
babibean,复杂类型也可以,比如对象,可以用nil传进去
 
可选参数“只能”放在最后
 
如下不知可不可以。。。


//定义
procedure MsgBox1(Text: string
const Caption: string = '操作提示');
begin
Application.MessageBox(PChar(Text),PChar(Caption),MB_OK+MB_ICONASTERISK);
end;

//调用1
MsgBox1'dddd');

//调用2
MsgBox1('11','22');
 
You can specify default parameter values in a procedure or function heading. Default values are allowed only for typed const and value parameters. To provide a default value, end the parameter declaration with the = symbol followed by a constant expression that is assignment-compatible with the parameter抯 type.
For example, given the declaration

procedure FillArray(A: array of Integer
Value: Integer = 0);

the following procedure calls are equivalent.

FillArray(MyArray);

FillArray(MyArray, 0);

A multiple-parameter declaration cannot specify a default value. Thus, while

function MyFunction(X: Real = 3.5
Y: Real = 3.5): Real;

is legal,

function MyFunction(X, Y: Real = 3.5): Real
// syntax error

is not.
Parameters with default values must occur at the end of the parameter list. That is, all parameters following the first declared default value must also have default values. So the following declaration is illegal.

procedure MyProcedure(I: Integer = 1
S: string)
// syntax error

Default values specified in a procedural type override those specified in an actual routine. Thus, given the declarations

type TResizer = function(X: Real
Y: Real = 1.0): Real;

function Resizer(X: Real
Y: Real = 2.0): Real;
var
F: TResizer;
N: Real;

the statements

F := Resizer;

F(N);

result in the values (N, 1.0) being passed to Resizer.
Default parameters are limited to values that can be specified by a constant expression. Hence parameters of a dynamic-array, procedural, class, class-reference, or interface type can have no value other than nil as their default. Parameters of a record, variant, file, static-array, or object type cannot have default values at all.
For information about calling routines with default parameter values, see Calling procedures and functions.

从帮助里面抄出来的,
 
多人接受答案了。
 
后退
顶部