Example:1
#include <iostream> using namespace std; void cal(int n1=10,int n2=20,int n3=30,int n4=40) { int s; s=n1+n2+n3+n4; cout<<"Sum ="<<s<<endl; } int main() { cal();//1 cal(1);//2 cal(1,2); //3 cal(1,2,3); //4 cal(1,2,3,4); //5 cal(10,2,5,3); //6 return 0; }
Output:
Sum =100
Sum =91
Sum =73
Sum =46
Sum =10
Sum =20
Example:2
#include <iostream> using namespace std; void line1(char ch='-',int n=10) { int i; for(i=1;i<=n;i++) { cout<<ch; } cout<<endl; } int main() { line1(); //1 - 10 times line1('*'); //2 * 10 times line1('$',5);//3 $ 5 times line1('#',30);//4 # 30 times return 0; }
Output:
———-
**********
$$$$$
##############################
Example:3
#include <iostream> using namespace std; void cal(int=5,int=2,int=32,int=20); int main() { cal();//1 cal(1);//2 cal(1,2); //3 cal(1,2,3); //4 cal(1,2,3,4); //5 cal(10,2,5,3); //6 return 0; } void cal(int n1,int n2,int n3,int n4) { int s; s=n1+n2+n3+n4; cout<<"Sum ="<<s<<endl; }
Output:
Sum =59
Sum =55
Sum =55
Sum =26
Sum =10
Sum =20