Develop a new think
Loading

ignou - Ques-ans - MCS-011

difference between call by reference and call by value

I just want to know the difference between call by value and call by reference in c and c++.

can anybody give me answer with example.
Ask By: bali - 2 yrs, 9 months ago

I send u the whole details about both in depth. Try to understand it.

Call by Value vs. Call by Reference:-

C passes parameters "by value" which means that the actual parameter values are copied
into local storage. The caller and callee functions do not share any memory -- they each
have their own copy. This scheme is fine for many purposes, but it has two disadvantages.

1) Because the callee has its own copy, modifications to that memory are not
communicated back to the caller. Therefore, value parameters do not allow the callee
to communicate back to the caller. The function's return value can communicate some
information back to the caller, but not all problems can be solved with the single
return value.


2) Sometimes it is undesirable to copy the value from the caller to the callee because the
value is large and so copying it is expensive, or because at a conceptual level copying
the value is undesirable.

The alternative is to pass the arguments "by reference". Instead of passing a copy of a
value from the caller to the callee, pass a pointer to the value. In this way there is only
one copy of the value at any time, and the caller and callee both access that one value
through pointers.

Some languages support reference parameters automatically. C does not do this -- the
programmer must implement reference parameters manually using the existing pointer
constructs in the language.

Swap Example:-

The classic example of wanting to modify the caller's memory is a swap() function
which exchanges two values. Because C uses call by value, the following version of
Swap will not work...

void Swap(int x, int y) { // NO does not work
int temp;
temp = x;
x = y; // these operations just change the local x,y,temp
y = temp; // -- nothing connects them back to the caller's a,b
}
// Some caller code which calls Swap()...
int a = 1;
int b = 2;
Swap(a, b);

Swap() does not affect the arguments a and b in the caller. The function above only
operates on the copies of a and b local to Swap() itself. This is a good example of how
"local" memory such as ( x, y, temp) behaves -- it exists independent of everything else
only while its owning function is running. When the owning function exits, its local
memory disappears.

Reference Parameter Technique:-

To pass an object X as a reference parameter, the programmer must pass a pointer to X
instead of X itself. The formal parameter will be a pointer to the value of interest. The
caller will need to use & or other operators to compute the correct pointer actual
parameter. The callee will need to dereference the pointer with * where appropriate to
access the value of interest. Here is an example of a correct Swap() function.

static void Swap(int* x, int* y) { // params are int* instead of int
int temp;
temp = *x; // use * to follow the pointer back to the caller's memory
*x = *y;
*y = temp;
}

// Some caller code which calls Swap()...
int a = 1;
int b = 2;
Swap(&a, &b);

Things to notice...

• The formal parameters are int* instead of int.
• The caller uses & to compute pointers to its local memory (a,b).
• The callee uses * to dereference the formal parameter pointers back to get the caller's
memory.

Since the operator & produces the address of a variable -- &a is a pointer to a. In
Swap() itself, the formal parameters are declared to be pointers, and the values of
interest (a,b) are accessed through them. There is no special relationship between the
names used for the actual and formal parameters. The function call matches up the actual
and formal parameters by their order -- the first actual parameter is assigned to the first
formal parameter, and so on. I deliberately used different names (a,b vs x,y) to emphasize
that the names do not matter.

Bigger Pointer Example :-

static void Swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
static void IncrementAndSwap(int* x, int* y) {
(*x)++;
(*y)++;
Swap(x, y); // don't need & here since a and b are already
// int*'s.
}
int main()
{
int alice = 10;
int bob = 20;
Swap(&alice, &bob);
// at this point alice==20 and bob==10
IncrementAndSwap(&alice, &bob);
// at this point alice==11 and bob==21
return 0;
}

I hope it is help u better to understand the difference between both.
OK. Bye. Good Luck & Have A Nice Day.....
Ans By: ARIJIT_VNS - 2 yrs, 9 months ago

Only Regiester Users can sumbit the Answer, Please Login