Can someone explain this output?

Hey guys, so I have this code from a question in my book, and i'm having trouble following along it's path to see why the output is 3_1_3_2_4_3_

Could someone explain what's happening here?
More specifically, what happens after 3_1_ is initially created?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static int e = 0;
int main(){
  int foo (int m, int & n);
  int b;
  b = 1;
  int x;
  for (int i = 0; i < 3; i++){
    x = foo(b + 2, b);
    cout << x << "_" << b << "_";
  };
  cout << endl;
  return 0;

}
int foo (int x, int & y){
  e++;
  y = e;

  return(x);
}


EDIT: edited to fix output mistake.
Last edited on
run your code step-by-step with a debugger.
watch the values of the variables and see where they change.

by the way, I have no idea what the book is trying to teach you there, the example seems awful.
It's a pretty confusing example, agreed, and it was a multi-part question asking me to underline/box/etc, where the formal arguments were, the formal parameters, and so on. But the last portion asked about the output, and i'm still lost as to how it got the values it says it did, even after setting break-points to read it. If you could express in words, maybe it'd be clearer because i'm still a bit lost.
Last edited on
Topic archived. No new replies allowed.