``````#include <cmath>
#include <iostream>

using namespace std;
void F2(int, int, int, int&, int&, int&);
int main ( void )

{
int X=1, Y=2, Z=3, A=4, B=5, C=6, A1=7, A2=8, A3=9;
F2(X, Y, Z, A, B, C);
cout << "X=" << X << "Y=" << Y <<endl;
cout << "Z=" << Z << "A=" << A <<endl;
cout << "B=" << B << "C=" << C <<endl;
cout << "A1=" << A1 << "A2=" << A2 << endl;
cout << "A3=" << A3 << endl;
system ("Pause");
return 0;
}

void F2(int X, int Y, int Z, int&A1, int& A2, int&A3)
{
X=X+5;
A1=A1*A1;
A2=(Y + Z)/2;
A3= (Y-Z)/2;
return;
}``````

the program outputs are X=1, Y=2, Z=3, A=16, B=2, C=0, A1=7, A2=8, and A3=9.
My question is how? For a quick example, in the function definition A1=A1*A1 which would be 49, why is the output 7. I'm assuming one I understand that one all the others would make sense. Thank you very much.

maybe you are getting confused with your very own variables...
Take it like this:
you firstly initialize the variables with.:

``int X=1, Y=2, Z=3, A=4, B=5, C=6, A1=7, A2=8, A3=9;``

Then you call a function F with arguments: (X, Y, Z, A, B, C) or (1,2,3,4,5,6)
Fine..
Now the function is called like (arguments X,Y,Z by value and arguments A,B,C by reference). If you are clear with difference between call by value and call by reference then follow..

``````//In your function, value pointed to by &A is now given by A1

void F2(int X, int Y, int Z, int&A1, int& A2, int&A3)
{
X=X+5; //no matter to X as called by value
A1=A1*A1;     // Value of A changes to A*A=4*4=16
A2=(Y + Z)/2; // Value of B changes to (2+3)/2=5/2=2
A3= (Y-Z)/2;  // Value of C changes to (2-3)/2=-1/2=0
return;
}``````

And as your main() function's values of A1,A2,A3 are not manipulated in any ways, they remain the same..

So the only thing I am calling from my function definition is A, B, and C which in my definition is A1, A2, and A3. And that is because of the int& in my main function?

A1 in your f2 function is actually using A and not A1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.