Call-By-Value versus Call-By-Refference

Reply

Join Date: Aug 2009
Posts: 23
Reputation: hket89 is an unknown quantity at this point 
Solved Threads: 0
hket89's Avatar
hket89 hket89 is offline Offline
Newbie Poster

Call-By-Value versus Call-By-Refference

 
0
  #1
Aug 24th, 2009
Could Someone tell me whats the diffrence between call-by-value with call-by-refference?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Call-By-Value versus Call-By-Refference

 
0
  #2
Aug 24th, 2009
Originally Posted by hket89 View Post
Could Someone tell me whats the diffrence between call-by-value with call-by-refference?
Hard time using a search engine?
http://www.lmgtfy.com/?q=pass+by+value+or+reference
Last edited by tux4life; Aug 24th, 2009 at 11:00 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Call-By-Value versus Call-By-Refference

 
1
  #3
Aug 24th, 2009
In general, passing by value means that a copy of variable is made with the same value and passing by reference means the variable object itself is passed. You can change the value all you want with pass by value, but the original object is not changed. With pass by reference any changes to the passed variable also change the original. C does not support passing by reference, but you can fake it with pointers:
  1. #include <stdio.h>
  2.  
  3. void CallByValue(int x)
  4. {
  5. x = 123;
  6. }
  7.  
  8. void CallByFakeReference(int* x)
  9. {
  10. *x = 123;
  11. }
  12.  
  13. int main()
  14. {
  15. int x = 0;
  16.  
  17. printf("Before CallByValue: %d\n", x);
  18. CallByValue(x);
  19. printf("After CallByValue: %d\n\n", x);
  20.  
  21. printf("Before CallByFakeReference: %d\n", x);
  22. CallByFakeReference(&x);
  23. printf("After CallByFakeReference: %d\n", x);
  24.  
  25. return 0;
  26. }
Faking call by reference is done by passing the address of an object and then accessing the value through indirection. That way you can get to the original object and make changes, but the address is still passed by value.
Last edited by Tom Gunn; Aug 24th, 2009 at 10:56 am.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Call-By-Value versus Call-By-Refference

 
0
  #4
Aug 24th, 2009
Originally Posted by tux4life View Post
Hard time using a search engine?
come on, man. that's a legitimate question to ask.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,170
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: Call-By-Value versus Call-By-Refference

 
0
  #5
Aug 24th, 2009
A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 23
Reputation: hket89 is an unknown quantity at this point 
Solved Threads: 0
hket89's Avatar
hket89 hket89 is offline Offline
Newbie Poster

Re: Call-By-Value versus Call-By-Refference

 
0
  #6
Aug 25th, 2009
What is the purpose of using call by value and call by reference?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Call-By-Value versus Call-By-Refference

 
1
  #7
Aug 25th, 2009
Originally Posted by hket89 View Post
What is the purpose of using call by value and call by reference?
Your question revealed that perhaps you are not understanding what it has been explained so far.
There's not such a thing as call by reference in The C language. Everything done is done by coping values around.
It is like a World where credit hasn't been invented. Anything you buy you need to pay with cash. The bigger the purchase, the more cash you have to carry around, the more cash you carry the more effort you have to make.
Pointers are like checks. Now you don't carry money around, but rather the small weight of a check book. And with a simple check you can provide the value of a much heavy and cumbersome pile of cash.

On the other hand. Call by reference in some other languages is like paying with Credit Cards.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,170
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: Call-By-Value versus Call-By-Refference

 
0
  #8
Aug 25th, 2009
Originally Posted by hket89 View Post
What is the purpose of using call by value and call by reference?
Again :

A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.
Pass by reference is a pointer under the hood.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Call-By-Value versus Call-By-Refference

 
0
  #9
Aug 25th, 2009
Pass by reference is a pointer under the hood.
There is no hood to look under. C does not have pass by reference, only pass by value. Pass by reference is faked in C by passing pointers by value and using indirection to get to the object.

A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.

You can think of Pass by reference like a pointer in disguise.
I think you are talking about C++, but even in C++, the main reason references are part of the language is to support operator overloading. They are also a convenient way to make output parameters, but that is a side effect of the original goal.
Last edited by Tom Gunn; Aug 25th, 2009 at 3:19 pm.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: Esthong is an unknown quantity at this point 
Solved Threads: 0
Esthong Esthong is offline Offline
Newbie Poster
 
0
  #10
Oct 14th, 2009
thanx
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC