+ 2

How do we pass the arguments in function declaration while using default argument?

https://code.sololearn.com/cV9V6h4llL8E/?ref=app

22nd Jul 2021, 7:01 AM
Nihal Kumar Das
Nihal Kumar Das - avatar
2 ответов
+ 3
#include <iostream> using namespace std; int sum(int x, int y, int z=0, int w=0) { return x+y+z+w; } int main() { cout << sum(10,15) << endl; cout << sum(10,15,25) << endl; cout << sum(10,15,25,30) << endl; return 0; }
22nd Jul 2021, 7:12 AM
Solo
Solo - avatar
+ 2
When writing the function body you don't need to add the default values again. #include <iostream> using namespace std; int sum(int, int, int z=5, int w=10); //int sum(int, int, int z, int w); int main() { cout << sum(10,15) << endl; cout << sum(10,15,25) << endl; cout << sum(10,15,25,30) << endl; return 0; } int sum(int x, int y, int z, int w) { return(x+y+z+w); }
22nd Jul 2021, 7:07 AM
Baribor Saturday
Baribor Saturday - avatar