Question

What is the output of the following program? #include <iostream> using namespace std; int fun (int p 2, int q = 3) return p+6. Please provide step by step and concepts explanation. Thank you !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here the output of this code is : 12

Step 1:

int fun(int p=2, int q=3);

return p+q

here they have taken the p=2 and q=3 as the argument and they return the p+q

Step: 2

cout<< fun()+fun(1)+fun(1,2) <<endl;

In the main function :

1.firstly the fun()

In this they does not have passed any argument so here we have to take the default argument and the default argument we have is the p=2 and q=3 and the the sum of p and q= 2+3=5.

2. fun(1)

in this they have passed the only one argument and that is 1.so, p=2 will be replaced by the 1. and we do not have the second argument so we have to take the default argument for the second argument in function and that is q=3.

so here p=1 and q=3

so, 1+3=4

3. fun(1,2)

so here they already given as both the argument so do not need to take the any default argument so here p=1 and q=3.

so, 1+2=3

NOTE: when you does not have passed any argument then you have to take the default argument

step-3

cout<< fun()+fun(1)+fun(1,2) <<endl;

fun()=5

fun(1)=4

fun(1.2)=3

by the addition of this three

so, the answer is 5+4+3 =12

so the output of the program is 12.

Add a comment
Know the answer?
Add Answer to:
6. Please provide step by step and concepts explanation. Thank you ! What is the output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT