Hi Folks,
1. Please help me in understanding the difference between these functions:
void func1(int& x) ;
void func2(const int& x);
OK, I can help with your first problem. In order to explain this, let's consider a third, simple case:
void func3(int x);
When calling func3, you have to provide it with an integer variable (such as x) or an integer value (such as 3). func3 then makes a copy of that data and stores it into a local variable called
x. For the duration of this function, you can use x as 3, or change the value of x. But whatever you do, when the function is over, whatever you used to call func3
has not changed.
Consider the following example:
- ...
-
int num = 5;
-
func3(num);
-
cout << "Back in main(): num = " << num; // outputs 5
-
...
-
void func3(int x) {
-
x = x + 5;
-
cout << "In func3(): x = " << x; // outputs 10
-
}
Even though I changed x inside func3, it did nothing to num in main! That's because func3 copied the information from num into x!
Now, consider your func1:
void func1(int& x);
A call to this function passes the argument by reference. Whatever happens to x in this function happens to the variable used to call it. When using a function header like this, you must pass an integer variable - it cannot be a value. THis is because the function call actually passes the address of the first variable, not its value.
Consider the following example:
- ...
-
int num = 5;
-
func1(num);
-
cout << "Back in main(): num = " << num; // outputs 10!
-
...
-
void func1(int& x) {
-
x = x + 5;
-
cout << "In func1(): x = " << x; // outputs 10
-
}
The address in memory that num refered to has been passed to func1 - thus, func1 is able to change the value stored there!
Finally, we come to your func2:
void func2(const int& x);
Now, because of the & symbol, we are again passing a variable by reference. However, there is a keyword const also. This means that, even though func2 has the address of num, it is not allowed to change it! The function can use that value in calculating other numbers, but it cannot assign anything new to x! This is useful for passing large variables. It would eat a lot of memory space to copy the entire variable into a new variable, so you pass by reference - but you don't want anything in that variable to be changed.
Consider the following example:
- ...
-
int num = 5;
-
func2(num);
-
cout << "Back in main(): num = " << num;
-
...
-
void func2(const int& x) {
-
x = x + 5; // Error! Can't change the value of x - it is constant!
-
cout << "In func2(): x = " << x;
-
}
My teacher once explained the difference in passing variables like this:
Suppose you, yourself, are a variable of type humanBeing. Now, if I were to pass you to a function
by value, I could take a picture of you. Now, I can use that picture to compare with other pictures or people, draw on it, cut it up, do whatever. But none of that affects you! It only affects your picture! Even after it's all been cut up, you can still take another picture and start over with it!
Now, if I were to pass you
by reference, I'd pass you - all of you. Now, if I decide to draw on you, I'm changing you - not your picture. I'm affecting you directly. To avoid this happening, I can say, "This humanBeing is constant!" That way, even though I'm passing you, I have forbidden anyone from changing you.
Hope that helped!