Nick
Nick

Reputation: 9

Can someone explain me how exactly this code is executed?

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;
}  

Upvotes: 0

Views: 212

Answers (6)

moldovean
moldovean

Reputation: 3261

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.

Upvotes: 0

mjwunderlich
mjwunderlich

Reputation: 1035

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.

Upvotes: 0

Vivian De Smedt
Vivian De Smedt

Reputation: 1029

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.

Upvotes: 0

Hassaan Salik
Hassaan Salik

Reputation: 413

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 .

Upvotes: 0

avinash pandey
avinash pandey

Reputation: 1381

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

Upvotes: 2

coder-don
coder-don

Reputation: 113

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;
}

Upvotes: 0

Related Questions