By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

C++ Function Arguments

P: 24
Hi Folks,


1. Please help me in understanding the difference between these functions:

void func1(int& x) ;
void func2(const int& x) ;


2. The following statements are valid in C++:

void function1(int** x);
void function1(int*& x);

How about the folowing? Are they valid? If yes, then how:
void function1(int&* x);
void function1(int&& x);
Nov 1 '06 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func3(num);
  4. cout << "Back in main(): num = " << num;   // outputs 5
  5. ...
  6. void func3(int x) {
  7.    x = x + 5;
  8.    cout << "In func3(): x = " << x;    // outputs 10
  9. }
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func1(num);
  4. cout << "Back in main(): num = " << num;   // outputs 10!
  5. ...
  6. void func1(int& x) {
  7.    x = x + 5;
  8.    cout << "In func1(): x = " << x;    // outputs 10
  9. }
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func2(num);
  4. cout << "Back in main(): num = " << num;
  5. ...
  6. void func2(const int& x) {
  7.    x = x + 5; // Error!  Can't change the value of x - it is constant!
  8.    cout << "In func2(): x = " << x;
  9. }
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!
Nov 1 '06 #2

P: 24
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func3(num);
  4. cout << "Back in main(): num = " << num;   // outputs 5
  5. ...
  6. void func3(int x) {
  7.    x = x + 5;
  8.    cout << "In func3(): x = " << x;    // outputs 10
  9. }
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func1(num);
  4. cout << "Back in main(): num = " << num;   // outputs 10!
  5. ...
  6. void func1(int& x) {
  7.    x = x + 5;
  8.    cout << "In func1(): x = " << x;    // outputs 10
  9. }
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. int num = 5;
  3. func2(num);
  4. cout << "Back in main(): num = " << num;
  5. ...
  6. void func2(const int& x) {
  7.    x = x + 5; // Error!  Can't change the value of x - it is constant!
  8.    cout << "In func2(): x = " << x;
  9. }
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!
--------------------------------------------


Thanks for the reply of the 1st part with an awesome explanation.

I got another doubt as follows, as you said:

Expand|Select|Wrap|Line Numbers
  1. void func2(const int& x);
  2.  
  3. 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! 
  4.  
In this case should the value of the variable "x" be constant or the Address?

Nov 2 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
Both, really. The address will never change and has never changed - they all refer to x. The value should not be allowed to change because it is constant (const).
Nov 2 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.