g++默认参数_C ++默认参数| 查找输出程序| 套装2
g++默認(rèn)參數(shù)
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 25Explanation:
說(shuō)明:
Here, we defined two functions fun() and sum().
在這里,我們定義了兩個(gè)函數(shù)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()函數(shù)返回全局變量 K的值。 函數(shù)sum()將第二個(gè)參數(shù)作為默認(rèn)參數(shù),這里我們將fun()用作Y的默認(rèn)值。 如果我們修改全局變量K的值,那么默認(rèn)值將自動(dòng)更改。
Now come to the function calls,
現(xiàn)在來(lái)看函數(shù)調(diào)用,
1st function call:
第一個(gè)函數(shù)調(diào)用:
A = sum(5);Here, X = 5 and Y =10, because the value of K is 10 till now. then function return 15.?
在此, X = 5且Y = 10 ,因?yàn)榈侥壳盀橹筀的值為10。 然后函數(shù)返回15。
2nd function call:
第二次函數(shù)調(diào)用:
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.
在第二個(gè)函數(shù)調(diào)用之前,我們修改全局變量K的值。 K的新值為20。然后X = 5,Y = 20。 然后函數(shù)sum()返回25。
Then the final output "15 25" will be printed on the console screen.
然后,最終輸出“ 15 25”將被打印在控制臺(tái)屏幕上。
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:
輸出:
230Explanation:
說(shuō)明:
Here, we defined function fun() that takes macro NUM as a default value of the argument.
在這里,我們定義了函數(shù)fun() ,該函數(shù)將宏NUM作為參數(shù)的默認(rèn)值。
Now, evaluate the expression used in the return statement,
現(xiàn)在,評(píng)估return語(yǔ)句中使用的表達(dá)式,
NUM*NUM 10+20*10+20 10+200+20 230Then function fun() will return 230, and that will be printed on the console screen.
然后,函數(shù)fun()將返回230 ,并將其打印在控制臺(tái)屏幕上。
Recommended posts
推薦的帖子
C++ Default Argument | Find output programs | Set 1
C ++默認(rèn)參數(shù)| 查找輸出程序| 套裝1
C++ Switch Statement | Find output programs | Set 1
C ++轉(zhuǎn)換語(yǔ)句| 查找輸出程序| 套裝1
C++ Switch Statement | Find output programs | Set 2
C ++轉(zhuǎn)換語(yǔ)句| 查找輸出程序| 套裝2
C++ goto Statement | Find output programs | Set 1
C ++ goto語(yǔ)句| 查找輸出程序| 套裝1
C++ goto Statement | Find output programs | Set 2
C ++ goto語(yǔ)句| 查找輸出程序| 套裝2
C++ Looping | Find output programs | Set 1
C ++循環(huán)| 查找輸出程序| 套裝1
C++ Looping | Find output programs | Set 2
C ++循環(huán)| 查找輸出程序| 套裝2
C++ Looping | Find output programs | Set 3
C ++循環(huán)| 查找輸出程序| 套裝3
C++ Looping | Find output programs | Set 4
C ++循環(huán)| 查找輸出程序| 套裝4
C++ Looping | Find output programs | Set 5
C ++循環(huán)| 查找輸出程序| 套裝5
C++ Arrays | Find output programs | Set 1
C ++數(shù)組| 查找輸出程序| 套裝1
C++ Arrays | Find output programs | Set 2
C ++數(shù)組| 查找輸出程序| 套裝2
C++ Arrays | Find output programs | Set 3
C ++數(shù)組| 查找輸出程序| 套裝3
C++ Arrays | Find output programs | Set 4
C ++數(shù)組| 查找輸出程序| 套裝4
C++ Arrays | Find output programs | Set 5
C ++數(shù)組| 查找輸出程序| 套裝5
翻譯自: https://www.includehelp.com/cpp-tutorial/default-argument-find-output-programs-set-2.aspx
g++默認(rèn)參數(shù)
總結(jié)
以上是生活随笔為你收集整理的g++默认参数_C ++默认参数| 查找输出程序| 套装2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: stl中map函数_map :: max
- 下一篇: c#读取指定字符后的字符_在C#中读取字