125k views
5 votes
What will the following code display? #include using namespace std; void doSomething(int); int main() { int x{2}; cout << x << '\\'; doSomething(x); cout << x << '\\'; return 0; } void doSomething(int num) { num = 0; cout << num << '\\';

User StackThis
by
5.8k points

1 Answer

5 votes

Answer:

The code will display the following on the screen:-

2

0

2

Step-by-step explanation:

The output is like this because x{2} assigns the value 2 to the integer x.First the value of x is printed on the screen that is 2 and that newline is used to go to the next line.After that function is called doSomething with x passed as the argument. doSomething function assigns the value zero to the variable passed and prints it on the screen.So the next line is 0.Since x is passed by value so no changes will appear in the original argument.Hence the last line printed is 2.

User Jason Terk
by
6.0k points