In the following C++ program, what value of x is printed after the function call? #include <iostream> using namespace std; void fun(int x, int y) { x = 20; y = 10; } int main() { int x = 10; fun(x, x); cout << x; return 0; }

Difficulty: Easy

Correct Answer: 10

Explanation:


Introduction / Context:
This question tests understanding of how arguments are passed to functions in C++. By default, parameters are passed by value, meaning that the function receives copies of the arguments. The example is slightly tricky because the same variable x is passed for both parameters, but the behaviour remains consistent with call by value semantics.


Given Data / Assumptions:

  • fun() is declared as void fun(int x, int y), so both parameters are passed by value.
  • Inside fun(), the local parameters x and y are assigned new values 20 and 10.
  • In main(), the integer x is initially 10 and fun(x, x) is called.
  • After the call, cout << x; prints the value of x in main.


Concept / Approach:
When you pass arguments by value, C++ copies the values of the arguments into the function parameters. Changes made to the parameters inside the function do not affect the original variables in the caller. Even though fun(x, x) supplies the same variable for both parameters, two independent copies are created. Modifying these copies does not change the original x declared in main().


Step-by-Step Solution:
Step 1: In main(), x is declared and initialized to 10. Step 2: The call fun(x, x) passes the value 10 for both parameters x and y in the function fun(). Step 3: Inside fun(), the parameters are local copies. Executing x = 20; and y = 10; modifies only these local copies. Step 4: When fun() ends, its local variables x and y are destroyed; no change has been made to main()'s x. Step 5: Back in main(), cout << x; prints the unchanged value 10.


Verification / Alternative check:
You can verify this behaviour by changing the function signature to use references, for example void fun(int &x, int &y). In that case, calling fun(x, x) would indeed modify main()'s x, and you would see a different result. The fact that the given program uses plain int parameters means that it uses call by value, which leaves the original x unchanged.


Why Other Options Are Wrong:
Option B (20) would be correct only if the function parameters were references or pointers that modified main()'s x directly. Option C is wrong because the code is valid C++ and compiles without error. Option D is wrong because "none of these" does not match the clearly determined output value 10.


Common Pitfalls:
A frequent source of confusion is passing the same variable to multiple parameters and assuming that changes to one parameter affect the other. With call by value, each parameter is independent. Another pitfall is forgetting the difference between passing by value and passing by reference, which can lead to subtle bugs when functions are expected to modify caller variables but do not. Always check function signatures carefully to understand argument passing semantics.


Final Answer:
The output of the program is 10, so the correct option is 10.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion