0

Can someone explain how the code is executed in here? I don't understand how the output in the second line is 8 7 8 and in the third line is 21 20 21.

#include<iostream>
using namespace std;
//Funtion passed by value and by reference
int fn1(int &a, int b, int *c){
   a=b++;
   b+=7;
   *c= ++a;

   return b;
}

int main(){
   int x=4, y=7, z=14;
   cout<< x<< " " << y<< " "<< z<< endl; // output: 4 7 14
   fn1(x,y,&z);
   cout<< x<< " " << y<< " "<< z<< endl; // output: 8 7 8 (I dont get this part!!!)
   x=9, y=12, z=19;

   y=fn1(x,y,&z);
   fn1(x,y,&z);
   cout<< x<< " " << y<< " "<< z<< endl; // output: 21 20 21(I dont get this part!!!)
   return 0;
}  
Mr M.
  • 715
  • 1
  • 8
  • 24
Nick
  • 9
  • 7
  • It is about the pointers. When you output the variables, it simply refers to allocated place instead of the defined variable. Check this out and you should have an idea : http://www.cplusplus.com/doc/tutorial/pointers/ – Hozikimaru Mar 31 '15 at 19:50
  • Why don't you get it? What do you think they should be? Remember the [post increment and pre increment return different values](http://stackoverflow.com/questions/17366847/what-is-the-difference-between-pre-increment-and-post-increment-in-the-cycle-fo) – NathanOliver Mar 31 '15 at 19:50
  • @NathanOliver I do not think it is because of the incremention really. I believe he needs to understand pointers vs usual variables. – Hozikimaru Mar 31 '15 at 19:51

6 Answers6

2

You are creating a reference for a passing b as it is and c's address

a is a reference to x hence any change in value of a is reflected in x
since y is passed by value any change in value of b will not change y
and for z as its address is passed any change to the location will be reflected in z

for a = b++
a gets the value 7 and b is incremented to 8(post increment)
b+=7
*c = ++a
a will become 8 and will get assigned to address pointed by c
hence you get output as 8 7 8 as x will be a y will remain 7 and z will be c

same for the next two calls

avinash pandey
  • 1,321
  • 2
  • 11
  • 15
  • Thanks for ur explanation. Can u explain the output in line 3? What is happening when we are saying that y=fn1(x,y,&z)? – Nick Mar 31 '15 at 20:00
  • value of b is first incremented in fn1 hence becomes 13 and then 7 is added to b which makes it 20.Now value of b is returned which is assigned to y.Hence after y=fn1(x,y,&z) x = 13 y = 20 and z = 13 now the second call to fn1(x,y,&z) has a = 13 b = 20 and c = 13 Now in the first line a=b++ a becomes 20 b becomes 21 in line b += 7 b becomes 28 and in line *c = ++a a = 21 and c = 21.As change is b is not reflected in y final values will be x = 21 y = 20 and z = 21 – avinash pandey Mar 31 '15 at 20:09
0

Second line:

x is modified by fn1 because the first parameter is passed by reference.

z is modified by fn1 because a pointer to z is passed to the function.

The first line:

a = b++

Assigns 7 to a because b is 7 and b++ is a post-increment function.

c = ++a

Increments a to 8 and assign 8 to c because ++a is a pre-increment function.

Same thing for line 3.

svlasov
  • 9,923
  • 2
  • 38
  • 39
Vivian De Smedt
  • 1,019
  • 2
  • 16
  • 26
0

While I'm not sure what you were expecting the outputs to be, I did see one thing that is probably not intentional.

Note that your function fn1 returns an int, however when you call it on line 20, it is not being used to update any of your variables. Perhaps you meant to update the value of y?

Also, you can get a better idea of how the function works by adding print statements to see how the internal variables iterate:

int fn1(int &a, int b, int *c){
   a=b++;
   cout << "a: " << a << " ";
   b+=7;
   cout << "b: " << b << " ";
   *c= ++a;
   cout << "c: " << *c << endl;

   return b;
}
coder-don
  • 113
  • 7
0

First output is pretty much simple, while in second output the only difference is that the return value of function i.e

b+=7

is used to rewrite the original value of Y. Then call the function again with new value of Y .

Hassaan Salik
  • 413
  • 4
  • 22
0

In your int fn1(int &a, int b, int *c) function:

a is passed in by reference.
b is passed in by value.
c is passed in by pointer.

a = b++; modifies x because a is a reference of x.
b += 7; modifies b but not y because b is just the value of y.
*c = ++a; modifies x and z because a is a reference of x, and c is a pointer to z.

When you use references and pointers, you can write directly values into their own memory space. However, when you use by value (think of b) a new variable is created within the current scope (inside of the fn1 function) and any changes to it are isolated to that scope.

Check out this SO answer to read more about scopes in C++. https://stackoverflow.com/a/11345787/1000437

Hope this helps.

Community
  • 1
  • 1
mjwunderlich
  • 1,025
  • 8
  • 13
0

I'll try to explain. The pointers are really fun and powerful if you have enough patience:

fn1(x,y,&z);
cout<< x<< " " << y<< " "<< z<< endl; 
// output: 8 7 8 (I dont get this part!!!)

Let's take a look at our function fn1

int fn1(int &a, int b, int *c){
a=b++;
b+=7;
*c= ++a;

return b;
}

First, note that in fn1(int &a) variable a is passed by reference, not by value. This means that we will directly operate with the value that we pass on a. It basically means that if you change the value of a inside the function, the change will persist. So since int x=4, y=7, z=14; and we call fn1(x, y, &z) , we pass the Variable x to a (think of it as renaming it temporarily from x to a). So a is initially 4. b is simply a value, so the contents of y is passed to b and *c is a pointer and simply takes as value the physical address of z (that's why you have fn1(&z)).

So when we call our function fn1(x, y, &z) we execute:

a=b++;

So a changes to 7, b changes to 8. (if this is confusing, read an intro about operators this) Then:

b+=7; //same as: b=b+7;

So b takes value 8+7=?? (sorry! I don't have a degree in Math!)

Finally:

*c= ++a;

Now, since we have ++a, first a changes to a=a+1 and now takes the value of 8, since it was 7! And the *c means "the value of the variable that pointer c points to": so the value of the variable that c points to is z. So the value of z is changed to whatever a is (which is 8).

And now the final step (for real this time). Why didn't y change? Because we fn1 takes b by value, not by reference, and when the function ends, var b gets destroyed.

Now try to do the second part:

 y=fn1(x,y,&z);

In this case y will take whatever fn1 returns! I'd really advise you take a step by step approach. If it's still confusing, exercise separately with the behaviour of each type of var passing it by ref, by val and later by pointer.

moldovean
  • 3,132
  • 33
  • 36