Ask Question

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. 9 December, 06:20
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers