g++默认参数_C ++默认参数| 查找输出程序| 套装1

g++默认参数

Program 1:

程序1:

#include <iostream>
using namespace std;

int sum(int X, int Y = 20, int Z = 30)
{
    return (X + Y + Z);
}

int main()
{
    int A = 0, B = 0;

    A = sum(5, 10);
    B = sum(10);

    cout << A << " " << B;

    return 0;
}

Output:

输出:

45 60

Explanation:

说明:

Here, we created a function sum(), here we use two default arguments Y and Z with values 20 and 30 respectively.

在这里,我们创建了一个函数sum() ,在这里我们使用两个默认参数YZ分别具有值20和30。

Default arguments mean, if we don’t pass any value for default argument then it takes default specified value.

默认参数意味着,如果我们不为默认参数传递任何值,则它将采用默认的指定值。

In the main() function, we declared two variables A and B with initial value 0.

main()函数中,我们声明了两个初始值为0的变量AB。

Here, we made two function calls:

在这里,我们进行了两个函数调用:

1st function call:
    A = sum(5,10);
    Here, X=5 and Y=10 and Z took default value 30. 
    Then A will be 45 after the function call.

2nd function call:
	B = sum(10);
    Here X=10 and Y and Z took default values 20 and 30 respectively. 
    Then B will be 60 after the function call.


Program 2:

程式2:

#include <iostream>
using namespace std;

int sum(int X = 10, int Y = 20, int Z)
{
    return (X + Y + Z);
}

int main()
{
    int A = 0, B = 0;

    A = sum(5, 10);
    B = sum(10);

    cout << A << " " << B;

    return 0;
}

Output:

输出:

main.cpp: In function ‘int sum(int, int, int)’:
main.cpp:4:5: error: default argument missing for parameter 3 of ‘int sum(int, int, int)’
 int sum(int X = 10, int Y = 20, int Z)
     ^~~

Explanation:

说明:

We can use only trailing argument as a default argument, that's why the above program will generate an error.

我们只能使用尾随参数作为默认参数,这就是上述程序将生成错误的原因。

Program 3:

程式3:

#include <iostream>
using namespace std;

int K = 10;

int sum(int X, int* P = &K)
{
    return (X + (*P));
}

int main()
{
    int A = 0, B = 20;

    A = sum(5);
    cout << A << " ";

    A = sum(5, &B);
    cout << A << " ";

    return 0;
}

Output:

输出:

15 25

Explanation:

说明:

Here, we defined a function sum() with a pointer as a default argument that stores the address of global variable K as a default value.

在这里,我们定义了一个函数sum() ,其指针作为默认参数,该参数将全局变量K的地址存储为默认值。

Here we made two function calls.

在这里,我们进行了两个函数调用。

1st function call:

第一个函数调用:

A = sum(5);

In this function call, returns (5+10), here the value of *P will be 10. Because pointer P contains the address of global variable K.  Then it will return 15. 

在此函数调用中,返回(5 + 10),此处* P的值为10。因为指针P包含全局变量K的地址。 然后它将返回15。

2nd function call:

第二次函数调用:

A = sum(5,&B);

In this function, we passed the address of B then return statement will be like this:

在此函数中,我们传递了B的地址,然后return语句将如下所示:

return (5+20)

Then the final values are 15 and 25 will print on the console screen.

然后,最终值1525将显示在控制台屏幕上。

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/cpp-tutorial/default-argument-find-output-programs-set-1.aspx

g++默认参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值