c++ - Why does the function always refer to one specific parameter? -
in following code:
int sum(int a=40, int b=20) { int result; result = + b; return (result); } int main () { int = 100; int b = 200; int result; result = sum(a, b); cout << "total value :" << result << endl; result = sum(a); cout << "total value :" << result << endl; return 0; } this produces:
total value : 300 total value : 120 why the:
sum(a) add (int a) in 2nd block (int b) in 1st block?
im confused why (b) value in 1st block used in (sum(a)), (a) value in 1st block ignored.
int sum(int a=40, int b=20) { ... } declares parameters a 40 , b to 20, if not specified. compiler service, sum(a) becomes sum(a, 20) (b not specified). similar this, sum() becomes sum(40, 20). a , b in method sum default parameters.
Comments
Post a Comment