Confusing code.

I dont understand how this code works. How did they get the variable z and why does proc1 print twice?

#include <iostream>
using namespace std;

const int X = 3;

void Proc1 (int &Y)
{
Y = 6;
cout << "Inside Proc1, X:" << X << " Y:" << Y <<"\n";
}

int Proc2(int &Y,const int Z)
{
int X=7;
Proc1(X);
Proc1(Y);
Y=3;
cout << "Inside Proc2,X: " << X << "Y: " << Y << "Z: " << Z << "\n";
return(Y+Z);}

int main()
{
int A=1;
int B=3;
int C=5;

C = Proc2 (A,B);
cout << "Finally,A: " << A << "B: " << B << "C: " << C << "\n";return(0);
}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Z is defined as one of the arguments to Proc2:

int Proc2(int &Y,const int Z)

3) Proc1 prints twice, because it's called twice:

1
2
Proc1(X);
Proc1(Y);


I get the impression that you don't yet understand even the fundamentals of what a function is and how you call them. I strongly recommend going back to the chapter in your textbook on functions, and re-reading it.
Topic archived. No new replies allowed.