Question

# Analyze the following programs and write down the output of the program. Please print every character...

Analyze the following programs and write down the output of the program. Please print every character (including whitespace character) clearly!

# include <iostream>

using namespace std;

int fun( int a )

{

int b = a * 2;

return b;

}

int main()

{

int y = 5;

cout << fun(y) << endl;

cout << fun(-- y) << endl;

cout << fun(y--) << endl;

cout << y <<endl;

return 0;

}

Output:

10

8

8

3

Execution Step by Step:

1. y = 5 assigned.
2. fun(5) is called.
3. fun(5) computes b= a*2, passed a=5. So, b=5*2=10.
4. Return value 10 is printed.
5. fun(--y) is called --y is prefix so the y = y-1 = 5-1=4 is done first then fun(4) is called
6. fun(4) computes b= a*2, passed a=4. So,return b=2*4=8
7. Return value 8 is printed.
8. fun(y--) is called y-- is postfix so the y=y stays and after the function execution y decremented by value 1. So fun(4) is called.
9. fun(4) computes b= a*2, passed a=4. So, returns b=2*4=8.
10. Return value 8 is printed.
11. y=y-1 is executed now. So y=y-1=4-1=3.
12. y is 3 here so 3 is the output will be printed.

#### Earn Coins

Coins can be redeemed for fabulous gifts.