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

what is pass by ref function?

arunmib
100+
P: 104
hi all,
I just got this freaky kind of doubt....I have the following piece of code,

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      int Val= 10, *ptr;
  4.      ptr = &Val;
  5.  
  6.     TestFn(&Val);
  7.  
  8.     TestPtr(ptr);
  9.  
  10.     return 0;
  11. }
  12.  
  13. void TestFn(int *i)
  14. {
  15.     ....does some operations
  16. }
  17.  
  18. void TestPtr(int *j)
  19. {
  20.    ....does some other operations
  21. }
Technically speaking for the first function (TestFn) I am passing the address of the variable Val. So I can call this function as pass by reference function.

But when it comes to the second function (TestPtr), what i am doing is the following, the address of the Val variable is assigned to Ptr. So, for second function we are just sending the address directly as input parameter. You people get my point right, the function's input parameter is a pointer and the input is also a pointer (with the address of another variable). So this just an equivalent to pass by value (not with values, but with addresses in pointer variables)

Can I call this as pass by value then ?

Am I clear or do I need to be more descriptive....
May 9 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 10K+
P: 11,448
C uses pass by value only; even when you pass a pointer as a parameter, the
value of that pointer is passed to the function. C++ also has a pass by reference
mechanism but that's not what you're using in your example code.

kind regards,

Jos
May 9 '07 #2

arunmib
100+
P: 104
Thanks for the reply Jos.

So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
May 9 '07 #3

Expert 100+
P: 102
Thanks for the reply Jos.

So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
Hi,
In C, pointers also occupy some place in the memory to store the address.
In C++ there is something called reference type. This is just like giving an alias/ new name to the variable.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. ...
  4. int i=5;
  5. ...
  6. int &j=i;
  7. ...
  8. }
  9.  
Here j does is not allocated with memory. Instead it refers to the same location of i. Consider the following pass-by-reference function call in C++:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. ..
  4. int i=5;
  5. ...
  6. call function(i);
  7. ...
  8. }
  9. void function(int &j)
  10. {
  11. ...
  12. }
  13.  
I hope this was helpful.

Regards,
Pradeep
May 9 '07 #4

AdrianH
Expert 100+
P: 1,251
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


Adrian
May 9 '07 #5

Expert 100+
P: 102
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


Adrian
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

Thanks,
Pradeep
May 10 '07 #6

AdrianH
Expert 100+
P: 1,251
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

Thanks,
Pradeep
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


Adrian
May 10 '07 #7

Expert 100+
P: 102
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


Adrian
Ok. Thank You Adrain.

Regards,
Pradeep
May 10 '07 #8

AdrianH
Expert 100+
P: 1,251
Ok. Thank You Adrain.

Regards,
Pradeep
No prob. Glad to help.


Adrian
May 10 '07 #9

arunmib
100+
P: 104
Hey thanks to adrian and pradeep for making thing clear.....
May 10 '07 #10

AdrianH
Expert 100+
P: 1,251
Hey thanks to adrian and pradeep for making thing clear.....
Glad to help.


Adrian
May 10 '07 #11

Post your reply

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