954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Call-By-Value versus Call-By-Refference

Could Someone tell me whats the diffrence between call-by-value with call-by-refference?

hket89
Light Poster
31 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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:

#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;
}

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.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
Hard time using a search engine?

come on, man. that's a legitimate question to ask.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

What is the purpose of using call by value and call by reference?

hket89
Light Poster
31 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
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
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

thanx

Esthong
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

The main reason to use pass by reference is to be able to access the original object so that it will still be modified when you leave the function. Right? (Not the best wording, but hopefully what I mean is clear). I see other statements in here that don't make sense to me.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You