#include <iostream>
using namespace std;
// this function increments the actual variable
// passed as an argument because it is passed
// as reference (with & in front of its name)
void correct_increment(int &i)
{
i++;
}
// this function does increment only a copy
// of the input argument, so the modifications
// are lost once the function terminates
void wrong_increment(int i)
{
i++;
}
// main function
int main()
{
int i=0;
// call wrong increment 3 times
wrong_increment(i);
wrong_increment(i);
wrong_increment(i);
// print out the result
cout << "value of i after 3 calls to wrong_increment: " << i << endl;
cout << endl;
// call wrong increment 3 times
correct_increment(i);
correct_increment(i);
correct_increment(i);
// print out the result
cout << "value of i after 3 calls to correct_increment: " << i << endl;
// return 0 from the main
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIHRoaXMgZnVuY3Rpb24gaW5jcmVtZW50cyB0aGUgYWN0dWFsIHZhcmlhYmxlCi8vIHBhc3NlZCBhcyBhbiBhcmd1bWVudCBiZWNhdXNlIGl0IGlzIHBhc3NlZAovLyBhcyByZWZlcmVuY2UgKHdpdGggJiBpbiBmcm9udCBvZiBpdHMgbmFtZSkKCnZvaWQgY29ycmVjdF9pbmNyZW1lbnQoaW50ICZpKQp7CiAgaSsrOwp9CgovLyB0aGlzIGZ1bmN0aW9uIGRvZXMgaW5jcmVtZW50IG9ubHkgYSBjb3B5Ci8vIG9mIHRoZSBpbnB1dCBhcmd1bWVudCwgc28gdGhlIG1vZGlmaWNhdGlvbnMKLy8gYXJlIGxvc3Qgb25jZSB0aGUgZnVuY3Rpb24gdGVybWluYXRlcwoKdm9pZCB3cm9uZ19pbmNyZW1lbnQoaW50IGkpCnsKICBpKys7Cn0KCi8vIG1haW4gZnVuY3Rpb24KCmludCBtYWluKCkKewogIGludCBpPTA7CgogIC8vIGNhbGwgd3JvbmcgaW5jcmVtZW50IDMgdGltZXMKCiAgd3JvbmdfaW5jcmVtZW50KGkpOwogIHdyb25nX2luY3JlbWVudChpKTsKICB3cm9uZ19pbmNyZW1lbnQoaSk7CgogIC8vIHByaW50I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