calculation problems, can't get the right output

I don't know why i=2 in the code below. Can someone explain how it became like that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   #include <iostream>
    #include <cstdarg>
    using namespace std;
    int calculate(int &val, int arg)
    {
            val *= arg;
            return arg;
    }
    
    int main()
    {
            int i = 1;
            int j = calculate(i,2);   // because i=1, hence j=calculate(1,2) which makes j=2, but why is i=2 as well?
            cout << i << j;   
            return 0;
    }
line 4: val is specified as passed by reference. When you modify val at line 6, you're modifying the caller's argument (i).
Topic archived. No new replies allowed.