g++默认参数_C ++默认参数| 查找输出程序| 套装1
g++默認(rèn)參數(shù)
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 60Explanation:
說明:
Here, we created a function sum(), here we use two default arguments Y and Z with values 20 and 30 respectively.
在這里,我們創(chuàng)建了一個(gè)函數(shù)sum() ,在這里我們使用兩個(gè)默認(rèn)參數(shù)Y和Z分別具有值20和30。
Default arguments mean, if we don’t pass any value for default argument then it takes default specified value.
默認(rèn)參數(shù)意味著,如果我們不為默認(rèn)參數(shù)傳遞任何值,則它將采用默認(rèn)的指定值。
In the main() function, we declared two variables A and B with initial value 0.
在main()函數(shù)中,我們聲明了兩個(gè)初始值為0的變量A和B。
Here, we made two function calls:
在這里,我們進(jìn)行了兩個(gè)函數(shù)調(diào)用:
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.
我們只能使用尾隨參數(shù)作為默認(rèn)參數(shù),這就是上述程序?qū)⑸慑e(cuò)誤的原因。
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 25Explanation:
說明:
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.
在這里,我們定義了一個(gè)函數(shù)sum() ,其指針作為默認(rèn)參數(shù),該參數(shù)將全局變量K的地址存儲(chǔ)為默認(rèn)值。
Here we made two function calls.
在這里,我們進(jìn)行了兩個(gè)函數(shù)調(diào)用。
1st function call:
第一個(gè)函數(shù)調(diào)用:
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.?
在此函數(shù)調(diào)用中,返回(5 + 10),此處* P的值為10。因?yàn)橹羔楶包含全局變量K的地址。 然后它將返回15。
2nd function call:
第二次函數(shù)調(diào)用:
A = sum(5,&B);In this function, we passed the address of B then return statement will be like this:
在此函數(shù)中,我們傳遞了B的地址,然后return語句將如下所示:
return (5+20)Then the final values are 15 and 25 will print on the console screen.
然后,最終值15和25將顯示在控制臺(tái)屏幕上。
Recommended posts
推薦的帖子
C++ Default Argument | Find output programs | Set 2
C ++默認(rèn)參數(shù)| 查找輸出程序| 套裝2
C++ Switch Statement | Find output programs | Set 1
C ++轉(zhuǎn)換語句| 查找輸出程序| 套裝1
C++ Switch Statement | Find output programs | Set 2
C ++轉(zhuǎn)換語句| 查找輸出程序| 套裝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 ++循環(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-1.aspx
g++默認(rèn)參數(shù)
總結(jié)
以上是生活随笔為你收集整理的g++默认参数_C ++默认参数| 查找输出程序| 套装1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试官:元素排序Comparable和C
- 下一篇: 电源变换适用于非独立源码_适用于非Non