Ask Question
12 September, 22:16

What is the output of the following program? #include using namespace std; void doSomething (int&); int main () { int x = 2; cout << x << endl; doSomething (x); cout << x << endl; return 0; } void doSomething (int& num) { num = 0; cout << num << endl; }

+2
Answers (1)
  1. 13 September, 01:09
    0
    The output will be as following:-

    2

    0

    0

    Explanation:

    The function doSomething accepts the argument as reference. So whatever changes are made in the function to the argument the changes will be reflected onto the original arguments.

    First the value of x is 2. Hence 2 is printed first.

    Then function doSomething is called that changes the value of x from 2 to 0 and then prints it. Hence 0 is printed in the newline.

    Then in the main function x is printed again the function changed it's value so 0 is printed.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “What is the output of the following program? #include using namespace std; void doSomething (int&); int main () { int x = 2; cout ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers