以其他值作为参数调用函数时,函数的默认参数是否会被覆盖?

Will a default parameter of a function be overwritten when calling the function with other value as parameter?

我正在 Delphi 中创建一个需要特定值作为参数的函数,除非在调用函数时设置了该值。在这种情况下,默认参数会被覆盖吗?

示例:

function ExampleFunction(b = 3, a){
  b*a = c
}
ExampleFunction(15,2)

是否会用给定的参数 (15) 替换默认参数 (3)?

您的代码无法编译。它的语法无效。看起来好像您是用 Pascal 和 C# 的某种混合体编写了代码。我建议你解决这个问题。

此外,默认参数必须出现在列表的最后。原因是默认参数允许您在调用函数时省略参数。当您这样做时,编译器会用默认值替换缺少的参数。因为参数是位置性的,所以不可能省略一个参数,而是传递列表中出现在它之后的另一个参数。

documentation,我敦促你再读一遍,它说:

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.

现在进入正题。如果您没有省略参数,即如果您提供了它,那么将使用您提供的值。

我们用一个实际编译的例子:

function Test(a: Integer; b: Integer = 42): Integer;
begin
  Result := a * b;
end;

然后

Test(2) = 84 // parameter b is omitted, default value passed

Test(4, 3) = 12