answersLogoWhite

0

What do you mean by call value and call by reference?

Updated: 8/19/2019
User Avatar

Wiki User

10y ago

Best Answer

Call by value and call by reference relate to the way in which arguments are passed into functions. The function itself determines which method is employed.

When a function expects an argument by value, the function makes a copy of the argument you pass. That is, you are passing the argument's value, not the argument itself. The value you pass is subsequently assigned to the function argument. If the function argument is an object (an instance of a class), then that class' copy constructor is automatically invoked and the copy is assigned to the function argument. The function argument remains in scope until the function returns, which then invokes the class' destructor. Since the function works with a copy of the argument that you passed, the function has no direct effect on the argument you passed.

Passing large and complex objects by value is clearly costly in terms of both performance and memory, but if you want the function to actually make changes to your object, then pass by value will not work unless you also return a copy of the altered object and assign this copy to your original object. That's two copy constructors, two destructors, and one assignment. This is where pass by reference comes in.

When you pass by reference you are passing the address of your argument to the function, and that address is assigned to the function argument. So unless the function argument is declared const, any changes made to the function argument will be reflected in the argument you passed. This is clearly more efficient than passing by value, since no copies need to be made. And when the function argument is declared const, you can be assured that your object's immutable members will not be altered by the function since the function can only call const member functions.

Passing by reference is clearly the preferred method of passing arguments into functions, particularly when those arguments are complex objects. In order to achieve pass by reference, the function argument needs to accept an address. Assigning the address to a pointer argument seems the obvious way of doing this, but in C++ there is a better way: use a reference!

C programmers often get confused when we speak of C++ references because a reference in C is simply another name for a pointer. But in C++, a reference is not a pointer at all. Keep in mind that a pointer is a variable (or a constant), and therefore a pointer has an address of its own because it needs somewhere to store the address that it is pointing at. The stored address is the pointer's value, and that value can be dereferenced provided it is not NULL. That is, dereferencing a pointer gives us the indirect value stored in the address that the pointer is referring to. Moreover, pointer variables can be assigned any address at any time; they can point anywhere, or they can point to NULL.

In C++, however, references behave differently to pointers. Once assigned, you cannot reassign a reference and they can never be NULL. Therefore you must assign a reference to an exiting object at the point of instantiation; you cannot declare and assign separately as you can with a pointer. This is because references do not store the address they refer to, they are simply aliases for the address itself. In other words, the address of a reference is the same as the reference itself. In many ways, a reference is not unlike a constant pointer, since it always refers to the same object, but even a constant pointer has its own address, a reference does not.

Now that we understand what a reference is, we can see how it can be applied to functions that expect an address. When we pass our argument to a pass by reference function, we are assigning the address of the argument to the function argument, just as we would if the function argument were a pointer. The difference is that we don't need to use the address-of operator to obtain the address since the function already knows to expect a reference. That address is then assigned to the function argument. While the argument remains in scope, we have two guarantees: the reference cannot be NULL (so we don't need to test for NULL as we would with a pointer) and the reference cannot be reassigned (so its effectively the same as a const pointer). Moreover, since it is not a pointer of any kind, there is no redirection involved. The reference can be treated just as if it were the argument we passed; they are one and the same after all, just different names.

To conclude, here are some examples of functions that employ pass by value and pass by reference:

Example 1: pass by value

MyObject foo(MyClass val)

{

val.set_data(42);

return(val);

}

In this example we are passing a complex object of type MyClass that has a set_data() method. The object we pass is left unaffected because val is a copy of the object we passed. However, if we placed trace code in the class copy constructor and the destructor, we'd find two copies are constructed and destroyed during this call, once to create val, and again to create the return value. val is destroyed first and the return value second. The return value is an automatic variable which we can immediately assign to another object (including the original argument we passed to the function if we wished) but beyond that the return value will cease to exist.

To call this function and assign the return value to the original object, we'd use the following:

MyClass x;

x = foo(x);

Example 2: pass by reference (via pointer)

void foo(MyClass* const ptr)

{

if( ptr )

{

ptr->set_data(42);

// (*ptr).set_data(42);

}

}

Note that the points-to operator (->) is simply sugar-coating for the dereference operator, as shown in the commented line. Note also that the const keyword applies to the pointer itself, not what it points to. Thus *ptr is variable, but ptr is not.

Here we're passing a reference by const pointer. Since pointers may be NULL we must ensure the pointer is non-NULL before we attempt to dereference it. Note that we do not need to return anything since ptr is assigned the address of the argument we passed, thus any changes made to the pointer's indirect value are reflected in the argument. In other words, ptr is an output argument.

In order to call this function, we must employ the address-of operator. E.g.,

MyClass x;

foo(&x);

Example 4: pass by reference

void foo(MyObject& ref)

{

ref.set_data(42);

}

Here we can see the C++ way of passing by reference. Unlike pointers we don't need to test for NULL, nor do we need to dereference. ref is guaranteed to always refer to a valid object. Moreover, the function call itself is simplified because we don't need to specify the address-of operator.

MyClass x;

foo(x);

Note that ref is simply an alias for x. If we examine the address-of ref (&ref) we will find it is exactly the same as the address-of x (&x). If we look at the address-of ptr (&ptr) in the previous example, we'd find that it was not the same as &x, but its value would be &x, proving that references are neither pointers nor variables of any kind.

Note that although these examples use complex objects, the same principal applies to primitives. The only difference is that there is no penalty in passing primitives by value as it's just as quick to pass by value as it is to pass by reference. It's just a question of whether you want changes reflected in the original argument or not.

As a final note, if a function expects pass by reference, but you do not wish changes to be reflected in your object, copy the object first and pass the copy instead. This is really no different to pass by value. However, if the function argument is a constant reference, you don't need to make a copy since the function cannot alter the object's immutable members.

You can also overload functions to pass by value and by reference, giving the best of both options. However, to achieve this, you need to use the pointer version of pass by reference. This is because the compiler cannot differentiate pass by value from pass by reference any other way.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What do you mean by call value and call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you combine call by value and call by reference programs in c?

Very easily: there is no call-by-reference in C.


Explain the passing parameters as function in Call by Value and Call by Reference and then distinguish between them?

Call by value essentially passes a copy of an object's value whereas call by reference essentially passes the object itself. Pass by reference is the preferred method whenever possible as call by value will automatically invoke the object's copy constructor, which is often unnecessary, especially if the object is not affected by the function call (pass by constant reference).


What do you call an unchanging value in a formula in excel?

It can be called a constant or fixed value. If it is not a value but a cell reference then it can be called an absolute reference.


What do ISDN devices use a call reference value for?

To identify a specific call


How many values you can return by call by value and call by reference at a time?

A function can only return one value, but it can modify its parameters if their type is 'in out' or 'out'.


Do functions get called by reference only?

No. Function parameters are passed by value. Always. Even the so called "call by reference" is a value - the value of the pointer or the address of the object - but what is placed in the parameter list is a value.


What is Call of Duty reference nsl mean?

No Stun Lock


What is the difference in function calling by value and calling by reference?

Call by Value:- In this method a copy of the variables is created and is updated time to time but not the actual memory location is updated.so when we make a call to the function we get old valuesCall by Reference:- In this method we access the variable by the reference of the memory location,so when we make call to the variable we get the updated values.


What mean by reference function in c?

Reference function has no meaning. Variables are passed to functions by reference or by value, depending on the function signature.


What are call by value and call by reference?

Call by value is where the argument value is copied to the formal parameter, which is then passed to the function. While the function is executing, it can see the copy of the argument, and it can modify it, if desired, but since it is a copy, it cannot modify the original argument.Call by reference is where the argument's address (or some kind of reference to it, see the clarification below) is copied to the formal parameter, which is then passed to the function. While the function is executing, it can see the original argument, and it can modify it, if desired.Note that, formally, C and C++ are always call by value. When we use so-called call by reference semantics, whether it is explicit like in C, or implicit like in C++, we are simply treating the address of the argument as the value that is copied, but when you get into the nitty gritty details of the calling sequence, it is always call by value.As a clarification, because terminology is critical here, what we do in C and C++ is actually call by value or call by address, not call by reference. The distinction is important when you get into managed heap languages like Java and .NET, where the formal parameter is actually a reference handle to some object in the heap, and not actually a value nor an address.


How can you change values in call by reference?

We don't call by reference, we call functions. The arguments passed to the function are passed (not called) either by value or by reference, depending upon the function signature (the prototype). When you pass by reference you are passing the actual variable, not a copy of the variable, thus the function can modify that variable's value directly. The only exception is when the parameter is declared a constant reference. Passing a pointer is essentially the same as passing by reference, however the pointer itself is passed by value. To pass a pointer by reference you must pass a pointer-to-pointer instead. Passing by value always copies the value, whether it is declared constant or not. But if it is declared constant, the function might as well accept a constant reference. Passing objects (instances of a class) by constant value will incur a performance penalty in making an unnecessary copy. If it is constant, there is little point in copying the object.


What is a reference variable in c plus plus?

A reference variable in C++ is a formal parameter of a function call that automatically dereferences itself, as if it were a pointer, into a reference to the original value in the calling routine. You declare the reference type in the function declaration and prototype, but the compiler automatically adds the reference (&) operator on call, and the dereference (*) operator on use.