Deduce Output explanation Help.

Q1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void some_fun();
int main()
{
	int y=2;
	static int z=3;
	some_fun();
	y+=10;
	z+=10;
	some_fun();
   system("Pause");
   return 0;
}
void some_fun()
{
	int y=2;
	static int z=3;
	cout<< y <<"\t"<<z<<endl;
	y++;
	z++;
}


OUTPUT :
2    3
2    4


Q2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int func(int&x , int y=10)
{
	if(x%y==0)
		return ++x;
	else
		return y--;
}
int main()
{
	int p=20,q=23;
	q=func(p,q);
	p=func(q);
	cout<<p<< " " << " " <<q<<endl;
	q=func(p);
	cout<<p<< " " << " "<<q<<endl;
	system("pause");
	return 0;
}


OUTPUT
10 23
11 11 



Anyone can tell me how this code works to get the following ouput for Q1 and Q2 ?
Last edited on
Q1
Variables declared on lines 8, 9 and their subsequent increments on lines 11, 12 are red-herrings - they are never passed to some_fun(). So lets set them aside and focus on some_fun().
Within some_fun, y (line 19) has automatic storage duration so that the memory used for it is freed when the function returns while z (line 20) is declared static i.e. it has static storage duration whhich means memory used for it persists for the entire time the program is running.
So when some_fun() returns on line 10 the memory used for y is freed (since it has automatic storage duration, the default) while z is still == 3 (as it is static). So the next some_fun() call (line 13) starts with y == 2 and z == 4 which is what the function prints as the output

Q2 ... coming shortly

Last edited on
Hi @DesmondLee,
Based on what I understand from this code, for Q2 I think it's this:
When you got p=20 and q=23, you attribute q=func(p, q);
This will make, inside the function, parameter p get the value of p (20), and y is 10 every time you call the function.
Since x%y in this case is 20%10 (which checks the if statement), p is incremented then returned (p=24), and q is the same value (23), because it was not a reference.
q=23

When you call it for p=func(q), it does this:
x=p (with reference, x=24)
y is an added variable inside the function (because func() was not called with 2 parameters) with the value 10 (y=10)
checking x%y means checking 24%10, which returns y--.
Since return ++y would have changed the value of y and THEN return it, return y++ means "return y then increment y's value". But since the function returns, then the value doesn't get to modify what p gets afterwards (only modifies a local variable in this case).
Therefore p=func(q) will result into p=10;
p=10

Now you have p=10 and q=23.
They are printed, and then it calls "q=func(p)".
Inside the function you have:
x=p (x=10)
y=10
the "x%y==0" statement checks, so the return is x++.
Since x here is p (passed as parameter with reference), it returns x's value (10) with increment (11), giving q the value 11.
Since x is a reference to p, then p is incremented once, making it 11.

p=11
q=11

So when they are printed, you have these values.
Hope this helps.
Q2
func() has a default argument y = 10 (line 5) that is used only when one argument is passed to the function, for eg on lines 16, 18. When 2 arguments are passed to func() the actual value of the second argument takes precedence over y = 10.

func (20, 23) returns 23 -- i.e. the function returns 23 and only then the value of the variable representing 23, here q is reduced by one since its a post-decrement operation, so q = 23 on line 15

line 16: func(23) implies we use the default value of y = 10 and since 23%10 != 0 the function returns the value of y which is then reduced by 1, so p = 10 and these are the values you see on line 17...

You should be able to work out the rest from here ....

Last edited on
@gunnerfunner @Troat Thanks for the reply! it gives me much more understanding now. Thanks a lot !
Topic archived. No new replies allowed.