g++默认参数_C ++默认参数| 查找输出程序| 套装2
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 25Explanation:
說明:
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 = 5且Y = 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:
輸出:
230Explanation:
說明:
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 230Then 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stl中map函数_map :: max
- 下一篇: moore和mealy_Mealy机和M