# inlcude <iostream>
using namespace std;
void test(int &, int =1, int =1);
void test( int, int *, int&);
const int SIZE = 4;
int main()
{
int first=1. second = 2, third = 3;
int Numbers[SIZE]={2,4};
test(third, first);
cout << first << "," << second << "," << third << ","<< endl;
for (int i = 0; i< SIZE; i++)
cout << Numbers[i] << "," ;
test(third, &third, second);
cout << endl << first << "," << second << "," << third << "," << endl;
return 0;
}
void test(int & first, int second, int third)
{
first += 1;
second += 2;
third += 3;
cout << first << "," << second << "," << third << "," << endl;
}
void test( int first, int * second, int & third)
{
first += 3;
*second += 2;
third += 1;
}
Now its suppose to display:
4,3,4
1,2,4
2,4,0,0
1,3,6
Now i got the first three displays but I am having trouble on how the program got the output 1,3,6? I keep looking through the program but I don't see how it can. If anyone can explain this to me I would appreciate it a lot.
In the fourth display, the second and third arguments in the call to test will update the variables back in main.
Before this call,
first is 1
second is 2
third is 4
test(third, &third, second);
1 2 3 4 5 6
void test( int first, int * second, int & third)
{
first += 3; // this does not change value of third in main - still 4
*second += 2; // this adds 2 to value of third in main, now 6
third += 1; // this adds 1 to second variable in main, now 3
}