I'm making a function in Delphi that needs a specific value as parameter, unless it is set when function is called. While te default parameter be overwritten in that case?
我在Delphi中创建一个需要特定值作为参数的函数,除非在调用函数时设置它。虽然在这种情况下会覆盖默认参数?
example:
function ExampleFunction(b = 3, a){
b*a = c
}
ExampleFunction(15,2)
Will the default parameter(3) be replaced with the given parameter(15)?
是否将默认参数(3)替换为给定参数(15)?
1 个解决方案
#1
6
Your code does not compile. Its syntax is invalid. It looks rather as though you have written the code in some hybrid of Pascal and C#. I suggest that you fix the question.
您的代码无法编译。它的语法无效。看起来好像你已经用Pascal和C#的混合编写了代码。我建议你解决这个问题。
What's more, default parameters must appear last in the list. The reason for that is that default parameters allow you to omit an parameter when calling the function. When you do that, the compiler substitutes the missing parameter with the default value. Because parameters are positional, it is not possible to omit a parameter, but then pass another parameter that appears after it in the list.
更重要的是,默认参数必须出现在列表的最后。原因是默认参数允许您在调用函数时省略参数。执行此操作时,编译器将缺少的参数替换为默认值。因为参数是位置参数,所以不能省略参数,但是然后传递列表中显示的其他参数。
The documentation, which I urge you to read one more time, says:
我敦促你再读一遍的文件说:
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.
具有默认值的参数必须出现在参数列表的末尾。也就是说,第一个声明的默认值之后的所有参数也必须具有默认值。
Now to the question. If you do not omit the parameter, that is if you provide it, then the value you provided is used.
现在回答这个问题。如果不省略参数,即如果提供参数,则使用您提供的值。
Let's use an example that actually compiles:
让我们使用一个实际编译的例子:
function Test(a: Integer; b: Integer = 42): Integer;
begin
Result := a * b;
end;
Then
Test(2) = 84 // parameter b is omitted, default value passed
and
Test(4, 3) = 12
#1
6
Your code does not compile. Its syntax is invalid. It looks rather as though you have written the code in some hybrid of Pascal and C#. I suggest that you fix the question.
您的代码无法编译。它的语法无效。看起来好像你已经用Pascal和C#的混合编写了代码。我建议你解决这个问题。
What's more, default parameters must appear last in the list. The reason for that is that default parameters allow you to omit an parameter when calling the function. When you do that, the compiler substitutes the missing parameter with the default value. Because parameters are positional, it is not possible to omit a parameter, but then pass another parameter that appears after it in the list.
更重要的是,默认参数必须出现在列表的最后。原因是默认参数允许您在调用函数时省略参数。执行此操作时,编译器将缺少的参数替换为默认值。因为参数是位置参数,所以不能省略参数,但是然后传递列表中显示的其他参数。
The documentation, which I urge you to read one more time, says:
我敦促你再读一遍的文件说:
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.
具有默认值的参数必须出现在参数列表的末尾。也就是说,第一个声明的默认值之后的所有参数也必须具有默认值。
Now to the question. If you do not omit the parameter, that is if you provide it, then the value you provided is used.
现在回答这个问题。如果不省略参数,即如果提供参数,则使用您提供的值。
Let's use an example that actually compiles:
让我们使用一个实际编译的例子:
function Test(a: Integer; b: Integer = 42): Integer;
begin
Result := a * b;
end;
Then
Test(2) = 84 // parameter b is omitted, default value passed
and
Test(4, 3) = 12