g++默认参数

Program 1:

程序1:

#include <iostream>
using namespace std;
int K = 10;
int fun()
{
return K;
}
int sum(int X, int Y = fun())
{
return (X + Y);
}
int main()
{
int A = 0;
A = sum(5);
cout << A << " ";
K = 20;
A = sum(5);
cout << A << " ";
return 0;
}

Output:

输出:

15 25

Explanation:

说明:

Here, we defined two functions fun() and sum().

在这里,我们定义了两个函数fun()sum()

The fun() function returns the value of global variable K. and function sum() take the second argument as a default argument, here we use fun() as a default value of the Y. If we modify the value of global variable K, then the default value will be changed automatically.

fun()函数返回全局变量 K的值。 函数sum()将第二个参数作为默认参数,这里我们将fun()用作Y的默认值。 如果我们修改全局变量K的值,那么默认值将自动更改。

Now come to the function calls,

现在来看函数调用,

1st function call:

第一个函数调用:

A = sum(5);

Here, X = 5 and Y =10, because the value of K is 10 till now. then function return 15.

在此, X = 5Y = 10 ,因为到目前为止K的值为10。 然后函数返回15。

2nd function call:

第二次函数调用:

A = sum(5);

Before the second function call, we modify the value of the global variable K. The new value of K is 20. Then X = 5 and Y =20. Then function sum() return 25.

在第二个函数调用之前,我们修改全局变量K的值。 K的新值为20。然后X = 5,Y = 20。 然后函数sum()返回25。

Then the final output "15 25" will be printed on the console screen.

然后,最终输出“ 15 25”将被打印在控制台屏幕上。

Program 2:

程式2:

#include <iostream>
#define NUM 10 + 20
using namespace std;
int fun(int X = NUM)
{
return (NUM * NUM);
}
int main()
{
int RES = 0;
RES = fun();
cout << RES;
return 0;
}

Output:

输出:

230

Explanation:

说明:

Here, we defined function fun() that takes macro NUM as a default value of the argument.

在这里,我们定义了函数fun() ,该函数将宏NUM作为参数的默认值。

Now, evaluate the expression used in the return statement,

现在,评估return语句中使用的表达式,

NUM*NUM
10+20*10+20
10+200+20
230

Then function fun() will return 230, and that will be printed on the console screen.

然后,函数fun()将返回230 ,并将其打印在控制台屏幕上。

Recommended posts

推荐的帖子

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

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

  • 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-2.aspx

g++默认参数

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

  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. [CocoaPods]常见问题
  2. Android之使用ACTION_USAGE_ACCESS_SETTINGS权限检测手机多少天没有未使用其它APP
  3. 牛客题霸 [二叉树的最大深度]C++题解/答案
  4. java ee打印功能_Java EE 8的前5个新功能
  5. 基于SpringBoot 2.0正式版的SpringCloud的微服务实战项目搭建
  6. 在python中sqrt是什么意思_python中sqrt是什么意思
  7. mooc作业怎么上传附件_中国大学MOOC最新考试系统(老师视角)增加学生作弊成本...
  8. 推荐几个代码静态分析工具
  9. 活动喵怎么用?定向寻宝活动设计思路和实操(附2019最新教程)
  10. Win 7扫雷时间基址查找
  11. 双耳节拍 枕头_枕头2-3-0不在
  12. 用foobar,ape转mp3
  13. Java 如何给现有PDF文档添加页码
  14. 服务器安装系统要如何载入驱动程序,Dell 服务器安装Windows 2008 R2时手动加载阵列卡驱动...
  15. 程序员是否合格----看看键盘就知道了
  16. 用python使用py2neo时候报“ModuleNotFoundError:No module named 'py2neo'”的错误
  17. 编译原理(4):语法分析(自上而下)
  18. 美国泛达网络:新一代通用型数据中心机柜
  19. 凤凰系统无法更新play服务器,进不去系统怎么解决,点击进入后就重启,win10和凤凰双系统...
  20. scapy刺探星巴克无线网,记录顾客信息

热门文章

  1. java 审批流_一文读懂工作流
  2. 编写一个能够排序的函数模板。_LeetCode刷题——9.给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合...
  3. 上传 mp4 格式判断_视频如何转换成通用的MP4格式?按下这个键,10秒就能搞定...
  4. mac mysql prefpane_【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
  5. linux编辑java文本,linux下的文本编辑器VI的使用命令
  6. 北京科技大学计算机硕士,北京科技大学计算机专业硕士只有面授上课吗
  7. Linux下的一些简单网络配置命令介绍
  8. java sleep和wait区别
  9. MySQL 使用Node.js异步查询结果为undefined的简单处理办法
  10. Grafana文档(在Centos / Redhat上安装)