| | |
Call-By-Value versus Call-By-Refference
![]() |
•
•
•
•
Could Someone tell me whats the diffrence between call-by-value with call-by-refference?
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."
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:
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.
c Syntax (Toggle Plain Text)
#include <stdio.h> void CallByValue(int x) { x = 123; } void CallByFakeReference(int* x) { *x = 123; } int main() { int x = 0; printf("Before CallByValue: %d\n", x); CallByValue(x); printf("After CallByValue: %d\n\n", x); printf("Before CallByFakeReference: %d\n", x); CallByFakeReference(&x); printf("After CallByFakeReference: %d\n", x); return 0; }
Last edited by Tom Gunn; Aug 24th, 2009 at 10:56 am.
-Tommy (For Great Justice!) Gunn
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.
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? Ask4Answer
•
•
•
•
What is the purpose of using call by value and call by reference?
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.
•
•
•
•
What is the purpose of using call by value and call by reference?
•
•
•
•
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? Ask4Answer
•
•
•
•
Pass by reference is a pointer under the hood.
•
•
•
•
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.
Last edited by Tom Gunn; Aug 25th, 2009 at 3:19 pm.
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Returned value from first Ajax call is not shown in the second ajax call (JavaScript / DHTML / AJAX)
- How to call system calls in VC++? (C++)
- call by address (C++)
- New to Win32 ASM programming (Assembly)
- RollBack Transaction (VB.NET)
- Help with converting temperature (Assembly)
Other Threads in the C Forum
- Previous Thread: Split string into char array
- Next Thread: hardware programming
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






