1
#include <iostream>
using namespace std;

int fun1(int p){
     ++p;
     return p++;
}
int fun2(int &p){
    ++p;
    return p++;
}

int main(void){
int a = 1, b, c;
b = fun1(a);
c = fun2(b);
cout<< a + b + c << endl;
return 0;
}

The answer I get without running the program is 6 (a=1, b=2, c=3). However, after you run the program the answer returns 8 (a=1, b=4, c=3).

Please can somebody explain how you get to each answer?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Zohan
  • 11
  • 4
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Jun 09 '21 at 12:33
  • I've done this and it shows that a=1, b=4, c=3 - But when I try to figure out these answers without running the code I have no clue why b=4. – Zohan Jun 09 '21 at 12:35
  • _"The answer I get without running the program is 6"_ What does this mean? How do you get an answer without running the program? –  Jun 09 '21 at 12:36
  • [Hint] Look at the function signatures. What does it mean in `fun2` that it has `int &p` as the parameter? – NathanOliver Jun 09 '21 at 12:36

2 Answers2

1

After this call

b = fun1(a);

a will be unchanged because it is passed by value to the function and b will be equal to 2.

In this call

c = fun2(b);

the variable b is incremented twice within the function because it is passed to the function by reference

int fun2(int &p){
    ++p;
    return p++;
}

So b will be equal to 4 after calling the function but the variable c will be equal to 3 because the value of the post-increment operator

    return p++;

is the value of its operand before incrementing.

So you have

1 + 4 + 3
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

First you need to know about pre increment and post increment and also addresses

I'll explain your code function by function

so in fun1, function when you pass a (a=1) as p ++p is a pre increment method it first increment p by 1 then returns p , so here p is now 2 in next line when you return p++, p++ is a post increment method i.e. returns first then increments by 1. so fun1 returns 2

and value of b becomes 2;

now moving on to next function

here please note that you have passed reference to p not just p i.e. if you change p here, original p ( reference to variable you have passed) will also change So here you do ++p its pre increment so it increases the value of p by 1, please note you have passed b as p, value of b will also change from 2 to 3, now in next line, you return p++, 3 is returned as the value of fun2. Please note p++ returns 3 first, but also increases p by 1, since you passed the reference of p as b, b increases by 1, b becomes equal to 4.

I think its clear now,

Mohak Gupta
  • 163
  • 1
  • 9