+ 2
How do we pass the arguments in function declaration while using default argument?
2 Respostas
+ 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;
}
+ 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);
}