943,608 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1335
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 24th, 2009
0

Call-By-Value versus Call-By-Refference

Expand Post »
Could Someone tell me whats the diffrence between call-by-value with call-by-refference?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
hket89 is offline Offline
31 posts
since Aug 2009
Aug 24th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by hket89 ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Aug 24th, 2009
1

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

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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Aug 24th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by tux4life ...
Hard time using a search engine?
come on, man. that's a legitimate question to ask.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 24th, 2009
0

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

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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 25th, 2009
0

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

What is the purpose of using call by value and call by reference?
Reputation Points: 10
Solved Threads: 0
Light Poster
hket89 is offline Offline
31 posts
since Aug 2009
Aug 25th, 2009
1

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

Click to Expand / Collapse  Quote originally posted by hket89 ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 25th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by hket89 ...
What is the purpose of using call by value and call by reference?
Again :

Quote ...
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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 25th, 2009
0

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

Quote ...
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.

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 14th, 2009
0
Re: Call-By-Value versus Call-By-Refference
thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Esthong is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: i need help 2 solve dis problem
Next Thread in C Forum Timeline: hardware programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC