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

推荐的帖子

  • C++ Default Argument | Find output programs | Set 2

    C ++默认参数| 查找输出程序| 套装2

  • C++ Switch Statement | Find output programs | Set 1

    C ++转换语句| 查找输出程序| 套装1

  • C++ Switch Statement | Find output programs | Set 2

    C ++转换语句| 查找输出程序| 套装2

  • C++ goto Statement | Find output programs | Set 1

    C ++ goto语句| 查找输出程序| 套装1

  • C++ goto Statement | Find output programs | Set 2

    C ++ goto语句| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 1

    C ++循环| 查找输出程序| 套装1

  • C++ Looping | Find output programs | Set 2

    C ++循环| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 3

    C ++循环| 查找输出程序| 套装3

  • C++ Looping | Find output programs | Set 4

    C ++循环| 查找输出程序| 套装4

  • C++ Looping | Find output programs | Set 5

    C ++循环| 查找输出程序| 套装5

  • C++ Arrays | Find output programs | Set 1

    C ++数组| 查找输出程序| 套装1

  • C++ Arrays | Find output programs | Set 2

    C ++数组| 查找输出程序| 套装2

  • C++ Arrays | Find output programs | Set 3

    C ++数组| 查找输出程序| 套装3

  • C++ Arrays | Find output programs | Set 4

    C ++数组| 查找输出程序| 套装4

  • C++ Arrays | Find output programs | Set 5

    C ++数组| 查找输出程序| 套装5

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

g++默认参数

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

  1. 小程序 || 语句_C ++开关语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {switch (printf( ...

  2. 结构化程序goto语句_C ++ goto语句| 查找输出程序| 套装1

    结构化程序goto语句 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; i ...

  3. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装2

    小程序 || 语句 Program 1: 程序1: #include <iostream> #include <stdio.h> using namespace std; in ...

  4. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {if (NULL) {cout ...

  5. 输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3

    输入输出数组元素的函数重载 Program 1: 程序1: #include <iostream> using namespace std; class Test { public: vo ...

  6. c语言指针++_C ++此指针| 查找输出程序| 套装3

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; class Test { int VAL; public: ...

  7. c语言指针++_C ++此指针| 查找输出程序| 套装1

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; int main() { int A = 10; this ...

  8. c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3

    c++重载++运算符 Program 1: 程序1: #include <iostream> using namespace std; class Test { public: int A ...

  9. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int A, B; fr ...

  10. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

最新文章

  1. A + B Problem
  2. .NET Entity Framework入门简介及简单操作
  3. mysql中文时好时坏_mysql索引的问题,时好时坏
  4. ATG中的定时Job处理
  5. gel和react哪个厉害_gel、react、boost三种材料的跑鞋,哪个更强?
  6. Java微服务(四)【idea配置本地maven】【中文idea版本】(手把手编写,超级详细)
  7. php去除img,PHP如何去除IMG标签?_后端开发
  8. 当自监督遇上语言-图像预训练,UC伯克利提出多任务框架SLIP
  9. mysql可以复用删除的记录吗_MySQL学习(八)删除表数据
  10. webstorm快捷键生成html页面,webStorm快捷键总结
  11. JSP文件怎么运行JAVA_jsp文件怎么运行
  12. 用计算机读取三菱PLC程序,电脑如何读取与保存三菱plc数据?
  13. HDU:2055 An easy problem
  14. 计算跑步时的热量消耗
  15. 天津美术学院2021级专升本新生报到前后相关要求
  16. docker更改映射端口(实践篇)
  17. 数字IC手撕代码-兆易创新笔试真题
  18. 在公路路肩施工阶段中机械设备与路平石模具配合显威力
  19. 杰普实训(前端)第一天知识点总结
  20. iOS 逆向编程(五)通过 (OpenSSH) Wifi 远程连接登录 iPhone

热门文章

  1. Linux基础(firewalld防火墙配置管理工具的图形用户界面)
  2. jpa transaction 回滚_我遇到的JPA中事务回滚的问题
  3. LDAP命令介绍---import-ldif
  4. Linux-实用快捷键操作
  5. ORACLE中创建如何创建表,并设置结构和默认值
  6. centos6.5安装python3.6
  7. Google再次从官方商店下架伪装成合法程序的恶意应用
  8. ffmpeg解析TS流
  9. k8s边缘节点_边缘计算,如何啃下集群管理这块硬骨头?
  10. JDK8那些惊为天人的新特性