Possible Duplicate:
C++: When to use References vs. Pointers
I'm pretty new programmer to the C/C++ languages and since I am coming from a background of C#, Java, JavaScript and a little bit of Visual Basic and Python, I am having a hard time to understand some of the things in C++.
I already know how to use reference and pointers and what they really mean, etc. But I don't understand why and where to use them. I know that references sometimes are used like this:
int x = 2;
void Square(int &value)
{
value = value*value;
}
Square(x);
cout << x << endl;
And the output will be 4.
I thought I don't quite understand why to do it that way and not do like this:
int x = 2;
int Square(int value)
{
value = value*value;
return value;
}
x = Square(x);
cout << x << endl;
Anyway, I hope someone may be able to help me understand why and where to use reference and pointers.