473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ Function Arguments

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
3 2274
Ganon11
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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

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

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
7
by: sfeher | last post by:
Hi All, Is there a way to preserve the arguments across functions? I have: <script> function myFirstFunction() { // arguments = 'param1'
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.