+ 1

How does this works?

include <iostream> using namespace std; void printsum(int a,int b=4) {  cout<<a+b<<endl; } int main() {  printsum(13); } \\answer 17

18th Oct 2017, 11:02 PM
Blue!!
Blue!! - avatar
3 Respostas
+ 2
When you specify default values for the parameters a function takes when defining the function - like you did with "int b = 4" - these values are used if the value is missing when the function is called. So in your example, you say that if no second value is given, the compiler will use the default value, here 4. So it couts a, which is 13, added with b, which is 4 by default, and as a result the answer is 17. These default values are only used if a value is missing upon call, e.g. if you say printsum (13, 17); the output will be 30.
18th Oct 2017, 11:37 PM
Shadow
Shadow - avatar
0
I'm not really sure why I got a downvote on my last response....I guess I need to clarify my answer better. Please read on to see if I can explain better: #include <iostream> using namespace std; void printSum(int a,int b=4) { cout<<a+b<<endl; } int main() { printSum(13); return 0; } In your code, you created a function protocol that defined the value of int b...you also specified that this function will take 2 int arguments...You also stated that one of the arguments (int b) has been declared to be a value of 4...when you declared the function definition in main, you omitted the new value for int b, so it stayed the same value of 4 which is why you got the result of 17. Since you specified in your function protocol that it would accept two integers, you could use the same code to come up with a different result...here is an example: #include <iostream> using namespace std; void printSum(int a,int b=4) { cout<<a+b<<endl; } int main() { printSum(13,14); return 0; } The above example will give you a result of 27. The reason it works this way is partly because you specified that you could take two int arguments in your function protocol, which is why I was able to insert the values of 13 and 14 into the function within the main program. The other reason that it works this way, is because the original value that was declared for int b has been overridden by the usage of the function in the main program. Now back to what I was talking about in my first post....C++ requires the values to be defined in a right to left manner when dealing with function protocols/prototypes/definitions....which ever way you want to call it. Here is an example of an illegal definition within a function protocol: #include <iostream> using namespace std; void printsum(int a = 4,int b) { cout<<a+b<<endl; } int main() { printsum(13); return 0; } You will end up with an error like this: main.cpp: In function 'void printsum(int, int)': main.cpp:4:6: error: default
21st Oct 2017, 2:19 PM
bob
bob - avatar
- 1
One thing to keep in mind is that when you initialize a variable (int b=4 in your example), the variable initialization goes from right to left, which is why your code works with the way you wrote it. Where your code would run into problems is if you were to declare your function line this... void printsum(int a= 4, int b) .... insert rest of code in this example you would get a compile error because you declared and defined int a but did not do so for int b
19th Oct 2017, 1:08 PM
bob
bob - avatar