c语言 默认参数_C ++中的默认参数

c语言 默认参数

Default arguments are the arguments that are passed in the function definition which is used by the compiler if no arguments are provided, at the time of function call.

默认参数是在函数定义中传递的参数,如果在调用函数时未提供任何参数,则编译器将使用该参数。

Below is the example of default argument:

下面是默认参数的示例:

#include <bits/stdc++.h>
using namespace std;

void print(int a, int b = 1, int c = 2, int d = 3)
{
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
}

int main()
{
    int a = 12, b = 13, c = 14, d = 15;
 
    //first function call
    cout << "only argument a passed\n";
    print(a);
 
    //second function call
    cout << "only arguments a,b passed\n";
    print(a, b);
 
    //third function call
    cout << "only arguments a,b,c passed\n";
    print(a, b, c);
 
    //fourth function call
    cout << "All arguments a,b,c,d passed\n";
    print(a, b, c, d);

    return 0;
}

Output:

输出:

only argument a passed
a: 12
b: 1
c: 2
d: 3
only arguments a,b passed
a: 12
b: 13
c: 2
d: 3
only arguments a,b,c passed
a: 12
b: 13
c: 14
d: 3
All arguments a,b,c,d passed
a: 12
b: 13
c: 14
d: 15

In the above example, we have used the default argument concept. This is an instance of operator overloading, where the resolution is done during the compile time.

在上面的示例中,我们使用了默认参数concept 。 这是运算符重载的一个实例,其中的解析是在编译时完成的。

So in case of the first function call, we only passed a single argument in the calling function which was a and the other arguments b, c, d took its default value.

因此,在第一个函数调用的情况下,我们仅在调用函数中传递了一个参数a,而其他参数bcd为其默认值。

In the case of the second function call, we only passed two arguments in the calling function which was a, b, and the other arguments c, d took its default value.

在第二个函数调用的情况下,我们仅在调用函数中传递了两个参数ab和其他参数cd为其默认值。

In the case of the third function call, we passed three arguments in the calling function which was a, b, c, and the other argument d took its default value.

在第三个函数调用的情况下,我们在调用函数中传递了三个参数abc ,而另一个参数d为其默认值。

In the case of the fourth function call, we passed all four arguments in the calling function thus no default argument value is taken.

在第四个函数调用的情况下,我们在调用函数中传递了所有四个参数,因此不采用默认参数值

But is the case of defining default arguments, we need to follow few rules so that compiler doesn't find the function overloading ambiguous.

但是在定义默认参数的情况 ,我们需要遵循一些规则,以便编译器不会发现函数重载是模棱两可的。

1) In the function definitions, if we declare default argument for one variable, then all the variables after that need to have default arguments.

1)在函数定义中,如果我们为一个变量声明默认参数 ,则此后的所有变量都需要具有默认参数

Like below is not a valid function definition using default arguments and will throw compilation error,

如下所示,它不是使用默认参数的有效函数定义,并且会引发编译错误,

int func(int a, int b=0, int c)
//This will throw error as b has assign default argument 
//value but c hasn't been though c comes after b.

//This it should be declared like:
int func(int a, int c ,int b=0)

2) The default value for arguments are being copied if the argument value is provided while calling the function

2)如果在调用函数时提供了参数值,则将复制参数的默认值

3) It shouldn't lead to ambiguity which will lead to a compilation error.

3)不应导致歧义,否则会导致编译错误。

翻译自: https://www.includehelp.com/cpp-tutorial/default-argument.aspx

c语言 默认参数

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值