Check a function that can take 1,2,3 or 4 arguments at a Time and Display It's Sum

23:13 0 Comments A+ a-

Problem:
Write function(s) to execute the following main()function.
int main()
{
cout << sum(1,2) << endl; // should display 3
cout << sum(1,2,3) << endl; // should display 6
cout << sum(1,2,3,4) << endl; // should display 10
return 0;

}
Solution:
Note: The green highlighted code is the required Function

# include <iostream>
using namespace std;
int sum(int a, int b = 0, int c = 0, int d = 0);//while making such fuction define only in prototype if definition is not mentioned alongwith

int main()
{
cout<<"Sum of 1 and 2 is : "<<sum(1,2)<<endl;
cout<<"Sum of 1 ,2 and 3 is : "<< sum(1,2,3) << endl; 
cout<<"Sum of 1 ,2 ,3 and 4 is : "<< sum(1,2,3,4) << endl; 


return 0;
}

int sum (int a,int b,int c,int d)
{
return a+b+c+d;

}