Q.Find and write the output of the following C++ program code: Note : Assume all required header files are already included in the program. void Convert(float &X, int Y=2) { X=X/Y; Y=X+Y; cout<<X<<"*"<<Y<<endl; } void main() { float M=15, N=5; Convert(M,N); Convert(N); Convert(M); }
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →This program demonstrates call-by-reference and default function arguments in C++. The Convert function modifies its first argument directly, leading to the output:
3*8
2.5*4
1.5*3
The core concepts at play here are call-by-reference for the first parameter X and default arguments for the second parameter Y. Understanding how these work is key to tracing the program's execution.
When a parameter is passed by reference (like float &X), any changes made to X inside the Convert function directly modify the original variable in the main function that was passed to it. This is different from call-by-value, where a copy is made, and the original variable remains unchanged.
The int Y=2 in the function signature means that if the Convert function is called with only one argument, the compiler will automatically use 2 as the value for Y. If two arguments are provided, the second argument will override this default value.
Let's trace the program's execution step-by-step:
-
Initial State in
main:float M = 15;float N = 5;
At this point,
Mholds15.0andNholds5.0. -
First Call:
Convert(M, N);- The
Convertfunction is called. X(a reference) is bound toM. So,Xnow refers toM's memory location, and its initial value is15.0.Y(a value parameter) receives the value ofN. So,Yis5.- Inside
Convert:X = X/Y;- This becomes
M = 15.0 / 5; M(andX) is updated to3.0.
- This becomes
Y = X+Y;- This becomes
Y = 3.0 + 5; Y(the local parameter) is updated to8.
- This becomes
cout<<X<<"*"<<Y<<endl;- This prints the current value of
X(which is3.0) followed by*and then the current value ofY(which is8). - Output:
3*8
- This prints the current value of
After this call,
Mis3.0, andNremains5.0. The localYinsideConvertis destroyed. - The
-
Second Call:
Convert(N);- The
Convertfunction is called. X(a reference) is bound toN. So,Xnow refers toN's memory location, and its initial value is5.0.- Since only one argument is provided,
Ytakes its default value of2. - Inside
Convert:X = X/Y;- This becomes
N = 5.0 / 2; N(andX) is updated to2.5.
- This becomes
Y = X+Y;- This becomes
Y = 2.5 + 2; Y(the local parameter) is updated to4.
- This becomes
cout<<X<<"*"<<Y<<endl;- This prints the current value of
X(which is2.5) followed by*and then the current value ofY(which is4). - Output:
2.5*4
- This prints the current value of
After this call,
Mis3.0, andNis2.5. The localYinsideConvertis destroyed. … - The
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.