Delphi中的函数和过程(默认值和可选参数)

时间:2021-01-13 10:56:58

EDIT:

Is there a better way of doing this ?

有更好的方法吗?

TPendingBets = class(TDataModule)
  private
  public
    function    GetBdy(out IdEvent : Integer                           )                                                           : Boolean; overload;
    function    GetBdy(out IdEvent : Integer; out idBetType : TBetTypes)                                                           : Boolean; overload;
    function    GetBdy(out IdEvent : Integer; out idBetType : TBetTypes; Out TotalOrgStake,Price : Double; out PriceError :Boolean): Boolean; overload;
  end;

implementation

////////////////////

function    TPendingBets.GetBdy(out IdEvent : Integer ): Boolean;
var idBetType : TBetTypes;
    TotalOrgStake,Price : Double;
    PriceError :Boolean;
begin
   result :=  GetBdy(IdEvent,idBetType,TotalOrgStake,Price,PriceError);
end;

////////////////////

function    TPendingBets.GetBdy(out IdEvent : Integer; out idBetType : TBetTypes): Boolean;
var TotalOrgStake,Price : Double;
    PriceError :Boolean;
begin
   result :=  GetBdy(IdEvent,idBetType,TotalOrgStake,Price,PriceError);
end;

////////////////////

function    TPendingBets.GetBdy(out IdEvent : Integer; out idBetType : TBetTypes; Out TotalOrgStake,Price : Double; out PriceError :Boolean): Boolean;
begin
    result :=  false;
    if cdBdy.Eof = False then
    begin
       IdEvent   :=   cdBdy.FieldByName(IdEvent ).AsInteger);
       idBetType :=   TBetTypes(cdBdy.FieldByName(idBetType ).AsInteger);
       //.
       //.
       result := True;
    end;  
end;

NOTE:

As my initial question was not very clear I have removed part of it

由于我最初的问题不是很清楚,我已经删除了部分内容

6 个解决方案

#1


I think the overload solution is quite a good solution, at least it is one. Other programming languages require you to declare functions with different names.

我认为过载解决方案是一个很好的解决方案,至少它是一个。其他编程语言要求您声明具有不同名称的函数。

Another approach may be a dynamic array parameter like:

另一种方法可能是动态数组参数,如:

type
  TExtendedDynArray = array of Extended;  // if not already declared elsewhere

function Fn1(out arr: TExtendedDynArray): Boolean;

#2


Overloading does not mean having 5 copies of the code in the function. You might have 5 functions, but 4 of them just call the main function with the correct parameters.

重载并不意味着在函数中有5个代码副本。您可能有5个函数,但其​​中4个函数只使用正确的参数调用main函数。

I generally don't like out parameters. Have you considered returning a record containing all the results?

我一般不喜欢参数。您是否考虑过返回包含所有结果的记录?

Fn1Result = record
  A: Extended;
  B: Integer;
  C, D, E, F: Extended
  S: String;
end;

Then declare your function like:

然后声明你的函数:

function Fn1: Fn1Result;
begin
  Fn1.A := C_ConstA;
  // etc. . . .
end;

Naturally I am assuming you are not using output parameters as input. Per the Delphi help:

当然我假设您没有使用输出参数作为输入。根据Delphi的帮助:

An out parameter, like a variable parameter, is passed by reference. With an out parameter, however, the initial value of the referenced variable is discarded by the routine it is passed to. The out parameter is for output only; that is, it tells the function or procedure where to store output, but doesn't provide any input.

out参数(如变量参数)通过引用传递。但是,使用out参数时,引用变量的初始值将被传递给它的例程放弃。 out参数仅用于输出;也就是说,它告诉函数或过程存储输出的位置,但不提供任何输入。

I get a funny feeling you are using output parameters as input parameters. Don't. Pass them as var if you want them to go both ways.

我觉得你使用输出参数作为输入参数很有趣。别。如果您希望它们双向传递,请将它们作为var传递。

#3


In addition to what Uwe wrote (and I agree that overloading is a better solution here; up voted), you should never presume an out parameter to have any value at all at the start of the function. That's what out means.

除了Uwe写的内容(我同意重载在这里是一个更好的解决方案;投票),你绝不应该假定out参数在函数的开头有任何值。这意味着什么。

#4


You can use overloaded function something like this

您可以使用这样的重载函数

function Fn1(Out A:string; B:integer=5) : boolean; overload;

function Fn1(Out A:string; B:integer = 5):boolean;超载;

function Fn1(Out A:string) : boolean; overload;

function Fn1(Out A:string):boolean;超载;

you must to define both functions.

你必须定义这两个功能。

#5


As Jim showed, you don't need to copy the code around when overloading.

正如Jim所示,你不需要在重载时复制代码。

I don't like much to mix parameters with default values and overloading, because (doing it without care) you can create a mess.

我不喜欢将参数与默认值和重载混合在一起,因为(无需小心)可以创建一个混乱。

Normally, if I have to support a lot of syntaxes:

通常,如果我必须支持许多语法:

1) I define a complete signature, with ALL PARAMETERS. Let's call it the 'master' one.

1)我用ALL PARAMETERS定义一个完整的签名。我们称之为'主'。

2) All overloads call that one. Or call one to another, but in the end every overload chain must call the 'master' one to execute the task.

2)所有重载都会调用那个。或者彼此调用,但最后每个重载链必须调用'master'来执行任务。

For me, overloads are "parameter crunchers" or "translators". Later, if I need something to modify the master behavior, I create new parameters on 'master' (which can have default values - in fact, mostly have).

对我来说,重载是“参数计算器”或“翻译器”。稍后,如果我需要修改主行为的东西,我会在'master'上创建新参数(它可以有默认值 - 实际上,大多数都有)。

If I need a overload with the new parameter, I create a new one and it simply pass it to the "master" procedure. The side effect is that the old ones continue to behave the same.

如果我需要使用new参数重载,我创建一个新参数,它只是将它传递给“master”过程。副作用是旧的继续表现相同。

#6


Default values can only be used for input parameters (that is: By value and const). For var and out parameters, default values are not possible.

默认值只能用于输入参数(即:按值和常量)。对于var和out参数,默认值是不可能的。

As Jim already pointed out: You can avoid duplicating the code by having most overloaded functions call the one which takes all parameters, so they are merely thin layers over the original function. From Delphi 2005 on you can declare them inline, so the compiler might actually generate more efficient code.

正如Jim已经指出的那样:你可以通过让大多数重载函数调用带有所有参数的函数来避免重复代码,因此它们只是原始函数的薄层。从Delphi 2005开始,您可以将它们内联声明,因此编译器实际上可能会生成更高效的代码。

#1


I think the overload solution is quite a good solution, at least it is one. Other programming languages require you to declare functions with different names.

我认为过载解决方案是一个很好的解决方案,至少它是一个。其他编程语言要求您声明具有不同名称的函数。

Another approach may be a dynamic array parameter like:

另一种方法可能是动态数组参数,如:

type
  TExtendedDynArray = array of Extended;  // if not already declared elsewhere

function Fn1(out arr: TExtendedDynArray): Boolean;

#2


Overloading does not mean having 5 copies of the code in the function. You might have 5 functions, but 4 of them just call the main function with the correct parameters.

重载并不意味着在函数中有5个代码副本。您可能有5个函数,但其​​中4个函数只使用正确的参数调用main函数。

I generally don't like out parameters. Have you considered returning a record containing all the results?

我一般不喜欢参数。您是否考虑过返回包含所有结果的记录?

Fn1Result = record
  A: Extended;
  B: Integer;
  C, D, E, F: Extended
  S: String;
end;

Then declare your function like:

然后声明你的函数:

function Fn1: Fn1Result;
begin
  Fn1.A := C_ConstA;
  // etc. . . .
end;

Naturally I am assuming you are not using output parameters as input. Per the Delphi help:

当然我假设您没有使用输出参数作为输入。根据Delphi的帮助:

An out parameter, like a variable parameter, is passed by reference. With an out parameter, however, the initial value of the referenced variable is discarded by the routine it is passed to. The out parameter is for output only; that is, it tells the function or procedure where to store output, but doesn't provide any input.

out参数(如变量参数)通过引用传递。但是,使用out参数时,引用变量的初始值将被传递给它的例程放弃。 out参数仅用于输出;也就是说,它告诉函数或过程存储输出的位置,但不提供任何输入。

I get a funny feeling you are using output parameters as input parameters. Don't. Pass them as var if you want them to go both ways.

我觉得你使用输出参数作为输入参数很有趣。别。如果您希望它们双向传递,请将它们作为var传递。

#3


In addition to what Uwe wrote (and I agree that overloading is a better solution here; up voted), you should never presume an out parameter to have any value at all at the start of the function. That's what out means.

除了Uwe写的内容(我同意重载在这里是一个更好的解决方案;投票),你绝不应该假定out参数在函数的开头有任何值。这意味着什么。

#4


You can use overloaded function something like this

您可以使用这样的重载函数

function Fn1(Out A:string; B:integer=5) : boolean; overload;

function Fn1(Out A:string; B:integer = 5):boolean;超载;

function Fn1(Out A:string) : boolean; overload;

function Fn1(Out A:string):boolean;超载;

you must to define both functions.

你必须定义这两个功能。

#5


As Jim showed, you don't need to copy the code around when overloading.

正如Jim所示,你不需要在重载时复制代码。

I don't like much to mix parameters with default values and overloading, because (doing it without care) you can create a mess.

我不喜欢将参数与默认值和重载混合在一起,因为(无需小心)可以创建一个混乱。

Normally, if I have to support a lot of syntaxes:

通常,如果我必须支持许多语法:

1) I define a complete signature, with ALL PARAMETERS. Let's call it the 'master' one.

1)我用ALL PARAMETERS定义一个完整的签名。我们称之为'主'。

2) All overloads call that one. Or call one to another, but in the end every overload chain must call the 'master' one to execute the task.

2)所有重载都会调用那个。或者彼此调用,但最后每个重载链必须调用'master'来执行任务。

For me, overloads are "parameter crunchers" or "translators". Later, if I need something to modify the master behavior, I create new parameters on 'master' (which can have default values - in fact, mostly have).

对我来说,重载是“参数计算器”或“翻译器”。稍后,如果我需要修改主行为的东西,我会在'master'上创建新参数(它可以有默认值 - 实际上,大多数都有)。

If I need a overload with the new parameter, I create a new one and it simply pass it to the "master" procedure. The side effect is that the old ones continue to behave the same.

如果我需要使用new参数重载,我创建一个新参数,它只是将它传递给“master”过程。副作用是旧的继续表现相同。

#6


Default values can only be used for input parameters (that is: By value and const). For var and out parameters, default values are not possible.

默认值只能用于输入参数(即:按值和常量)。对于var和out参数,默认值是不可能的。

As Jim already pointed out: You can avoid duplicating the code by having most overloaded functions call the one which takes all parameters, so they are merely thin layers over the original function. From Delphi 2005 on you can declare them inline, so the compiler might actually generate more efficient code.

正如Jim已经指出的那样:你可以通过让大多数重载函数调用带有所有参数的函数来避免重复代码,因此它们只是原始函数的薄层。从Delphi 2005开始,您可以将它们内联声明,因此编译器实际上可能会生成更高效的代码。