answersLogoWhite

0


Best Answer

When we swap values in function, that time "Call by value" swaps copied value not the exact values. so it doesn't reflect on main function. But in case of "Call by

reference", we swap actual value which is available at that reference or address.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

In call by value a copy of the variables is passed to the function, whereas in call by reference the address of the variables is passed to the function.

call by value and call by reference are terms used in Visual Basic but it is pass by value and pass by reference.

This looks like a question for Visual Basic rather than C.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Pass by value creates a temporary copy of the object being passed. Pass by reference passes the actual object itself.

If the parameter is declared constant, you can safely pass by reference. If it is declared non-constant, you should only pass by reference when you expect changes to the value to be reflected in the original object. Otherwise you should pass by value; only the copy is affected, not the original object.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You mean pass by value and pass by reference. Both terms refer to how arguments are passed to functions. Note that the arguments passed to a function are the actual arguments while the arguments received by the function are the formal arguments. Formal arguments are essentially local to the function and will fall from scope when the function returns.

Passing by value or by reference is determined by the function itself. If the formal argument is a reference, then you are passing the actual argument by reference. If the formal argument is a value, then you are passing the value of the actual argument.

To demonstrate the difference, consider the following function definitions in C++:

void byval(int x) {++x;}

void byref(int& y) {++y;}

In both cases, the formal argument is incremented. The difference becomes apparent when we call the functions:

int main()

{

int z=5;

std::cout<<"z is "<<z<<std::endl;

byval(z);

std::cout<<"z is "<<z<<" after calling byval(z)"<<std::endl;

byref(z);

std::cout<<"z is "<<z<<" after calling byref(z)"<<std::endl;

}

The output of this program is:

z is 5

z is 5 after calling byval(z)

z is 6 after calling byref(z)

When we call byval(z), the function receives the value of z and assigns that value to x. x and z are different variables so when x is incremented, z is unaffected.

When we call byref(z), the function receives a reference to z and assigns that reference to y. Now z and y refer to the same address thus when y is incremented, z is incremented. After all, they are one and the same variable but with different names. That is, y is an alternate name, an alias, for the variable named z.

We can achieve the same effect as byref() using pointers instead:

void byptr(int* p){++(*p);}

In this case we must call the function using the address-of operator (&):

byptr(&z);

Although the effect is the same as pass by reference, the pointer is actually being passed by value. That is, the function receives the address of z and assigns that value to the pointer p. We then increment the indirect value of p, and since p contains the address of z, we increment z.

It should be noted that C++ references are not the same as C++ pointers. Pointers are variables in their own right and require their own storage (32-bits on a 32-bit system). This storage is used to store the address of the memory being pointed to. References, on the other hand, are not variables and require no storage of their own: a reference is simply an alias, an alternate name for an existing variable.

In C, there is no distinction between a reference and a pointer: they are both pointers. Hence the term, dereferencing a pointer (indirectly accessing the memory being pointed at). Thus in C, we achieve pass by reference by passing pointers.

Languages that do not have an explicit pointer type will either provide a token (such as &) to enable pass by reference or, more typically, will use pass by reference by default. Java, for instance, uses pass by reference by default.

Passing complex objects by reference is always the preferred method. This is because passing by value requires the value be copied. For primitive types such as int and char this is not a problem because copying primitives is a constant time operation. But for complex data types, the overhead of copying (which invokes the object's copy constructor) and subsequently destroying the copy when it falls from scope is largely unnecessary. If the actual argument needs to be modified by the function then you have no option but to pass by reference. The only alternative is to return the copy and assign the copy's value to the actual argument, which simply increases the overhead. However, if the function does not need to modify the argument at all, then the argument can be declared constant. For example:

void byconstref(const int& z) {...}

The const keyword gives assurance to the caller that the immutable members of the actual argument will not be affected by the function. The immutable member of a const int is the value of the int itself. In more complex objects, there may be a mixture of mutable and immutable members, but only the mutable members can be affected by the function since mutable members are not considered vital to the underlying data structure.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

"Call by value" and "call by reference" are also known as "pass by value" and "pass by reference", respectively. These terms refer to the way in which an argument is passed to a function when we call that function.

Whenever a function receives an argument, memory must be set aside for that argument. That memory is typically allocated upon the thread's call stack and the argument is treated just as if it were a local variable or constant. We call this argument the "formal argument". The argument that we actually pass to the function from the calling code is known as the "actual argument". Whenever we pass an actual argument to a function, that function must copy the argument and assign its value to the formal argument.

The actual argument and the formal argument are independent of each other. They are physically stored in separate memory locations thus any changes made to the formal argument will have no effect whatsoever upon the actual argument. This is what we mean by pass by value because the function operates upon a copy of the actual argument's value, never the actual argument itself.

If we want the function to operate upon the actual argument itself, we cannot use pass by value, we must use pass by reference. This is achieved by passing the address of the argument. The address itself is still passed by value, but because that value is a memory address, both the function and the calling code can refer to the same object in memory.

In C, we use pointer variables to store memory addresses, thus in order to pass by reference, the function must accept a pointer type. the following example demonstrates both call by value and call by reference in C:

void f (int value) {

value *= 2;

}

void g (int* reference) {

(*reference) *= 2;

}

int main () {

int i = 42;

f (i); // call by value

assert (i==42);

g (&i); // call by reference

assert (i==84);

}

In C++, we can also use pointer variables to pass by reference, but C++ also has a native reference type, so we can either use pointers or references to pass by reference. (Behind the Scenes we are simply passing a pointer, but references are generally easier to work with than pointers). Here's the same example as above in C++:

void f (int value) {

value *= 2;

}

void g (int& reference) {

reference *= 2;

}

int main () {

int i = 42;

f (i); // call by value

assert (i==42);

g (i); // call by reference

assert (i==84);

}

Note that in C++, the calls to f and g are exactly the same. It is only by looking at the function signatures that we can see whether we are passing by value or by reference.

Generally, we use pass by reference whenever we pass an argument that is larger than a pointer variable. We do this because copying complex objects is expensive, both in terms of memory and performance. However, when a function does not need to change the logical representation of an object, we should always use constant references to make it clear to the caller the object will not be altered:

void g (const int& reference) {

reference *= 2; // error: reference is constant and cannot be modified

}

In C we can do the same thing using a pointer to constant type:

void g (int* const reference) {

(*reference) *= 2; // error: reference points to a constant and cannot be modified

}

Reference arguments are also used to return values to the caller (in addition to the function's actual return value, if any). These are known as output arguments. However, while common practice in C, this is considered bad programming style in C++. If a C++ function really must return more than one value, then it is better to return those values in a structure or class via the function's return value. In so doing, all reference arguments can be declared constant, thus assuring the caller that no changes will take place behind their back. If a change must be made, the function may copy the argument and then return the copy, allowing the caller to decide what to do with the modified object without affecting the original object in any way, all of which can be achieved using the pass by value semantic alone.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does call-by-Value and call-by-Reference mean?
Write your answer...
Submit
Still have questions?
magnify glass
imp