Computer Science, asked by rohitt4, 1 year ago

can someone help i cant get the difference between call by value and call by reference

Attachments:

Answers

Answered by athulbiju10
1

Difference between Call by Value and Call by ReferenceExample using Call by ValueExample using Call by Reference

There are two ways to pass arguments/parameters to function calls -- call by value and 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:

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 requiredIf 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 ReferenceDifference between call by value and call by referencecall by valuecall by referenceIn call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.In call by value, actual arguments will remain safe, they cannot be modified accidentally.In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.

athulbiju10: welcome
Answered by aksachan6121
0
HEYA....

best ever example I get to clear this...

hope u too can understand. .
if need any more assistance
just tell me...


hope it's helpful...

#itsme
Attachments:
Similar questions