0% found this document useful (0 votes)
12 views4 pages

Call by Val Ref

The document explains the differences between call by value and call by reference in C language, highlighting that call by value passes a copy of actual arguments while call by reference passes the address of the arguments, allowing modifications to reflect in the original variables. It provides examples demonstrating both methods, showing that call by value does not alter the original variables, whereas call by reference does. Additionally, it notes that when arrays are passed, the address of the array is used, allowing direct access to its elements.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Call by Val Ref

The document explains the differences between call by value and call by reference in C language, highlighting that call by value passes a copy of actual arguments while call by reference passes the address of the arguments, allowing modifications to reflect in the original variables. It provides examples demonstrating both methods, showing that call by value does not alter the original variables, whereas call by reference does. Additionally, it notes that when arrays are passed, the address of the array is used, allowing direct access to its elements.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is the difference between call by value and

call by reference in C language?


 Difference between Call by Value and Call by Reference
 Example using Call by Value
 Example using Call by Reference
There are two ways to pass arguments/parameters to function calls -- call
by valueand call by reference. The major difference between call by
value and call by reference is that in call by value a copy of actual
arguments is passed to respective formal arguments. While, in call by
reference the location (address) of actual arguments is passed to formal
arguments, hence any change made to formal arguments will also reflect
in actual arguments.

In C, all function arguments are passed "by value" because C does not
support references like C++ and Java do. In C, the calling and called
functions do not share any memory -- they have their own copy and the
called function cannot directly alter a variable in the calling function; it
can only alter its private, temporary copy.

The call by value scheme is an asset, however, not a liability. It usually


leads to more compact programs with fewer extraneous variables,
because parameters can be treated as conveniently initialized local
variables in the called routine. Yet, there are some cases where we
need call by reference:
1. The called function communicates to the calling function only
through return statement and return statement can only send only
one value back to the calling function. If there are more than one
value we want to alter, call by reference is required
2. If the size of data is large , copying actual arguments to formal
arguments could be a time consuming operation and occupies more
memory.
The call by value does not address above cases, hence we need call by
reference. To achieve call by reference functionality in C language the
calling function provides the address of the variable to be set (technically
a pointer to the variable), and the called function declares the parameter
to be a pointer and access the variable indirectly through it. Since the
address of the argument is passed to the function, code within the called
function can change the value of the actual arguments.

While studying call by value and call by reference in C it is important to


note that the story is different for arrays. When the name of an array is
used as an argument, the value passed to the function is the location or
address of the beginning of the array --there is no copying of array
elements. By subscripting this value, the function can access and alter
any element of the actual array.

Difference between Call by Value and Call by Reference


Difference between call by value and call by reference

call by value call by reference

In call by value, a copy of actual In call by reference, the location


arguments is passed to formal (address) of actual arguments is
arguments of the called function passed to formal arguments of
and any change made to the formal the called function. This means
arguments in the called function by accessing the addresses of
have no effect on the values of actual arguments we can alter
actual arguments in the calling them within from the called
function. function.

In call by reference, alteration to


In call by value, actual arguments
actual arguments is possible
will remain safe, they cannot be
within from called function;
modified accidentally.
therefore the code must handle
arguments carefully else you get
unexpected results.
Example using Call by Value
The classic example of wanting to modify the caller's memory is
a swapByValue()function which exchanges two values. For C uses call by
value, the following version of swap swapByValue() will not work...

#include <stdio.h>

void swapByValue(int, int); /* Prototype */


int main() /* Main function */
{
int n1 = 10, n2 = 20;

/* actual arguments will be as it is */


swapByValue(n1, n2);
printf("n1: %d, n2: %d\n", n1, n2);
}

void swapByValue(int a, int b)


{
int t;
t = a; a = b; b = t;
}

OUTPUT
======
n1: 10, n2: 20

The swapByValue() does not affect the arguments n1 and n2 in the calling
function it only operates on a and b local to swapByValue() itself. This is a
good example of how local variables behave.
Example using Call by Reference
In call by reference, to pass a variable n as a reference parameter, the
programmer must pass a pointer to n instead of n itself. The formal
parameter will be a pointer to the value of interest. The calling function
will need to use & to compute the pointer of actual parameter. The called
function will need to dereference the pointer with *where appropriate to
access the value of interest. Here is an example of a
correctswap swapByReference() function. So, now you got the difference
between call by value and call by reference!

#include <stdio.h>

void swapByReference(int*, int*); /* Prototype */

int main() /* Main function */


{
int n1 = 10, n2 = 20;

/* actual arguments will be altered */


swapByReference(&n1, &n2);
printf("n1: %d, n2: %d\n", n1, n2);
}

void swapByReference(int *a, int *b)


{
int t;
t = *a; *a = *b; *b = t;
}

OUTPUT
======
n1: 20, n2: 10

You might also like