answersLogoWhite

0


Best Answer

Call by reference, particularly when applied to objects, because call by value automatically invokes an object's copy constructor, which is seldom desirable when passing objects into functions.

User Avatar

Wiki User

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

Wiki User

8y ago

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address.

Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function.

In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

When you use passing by reference you save RAM because reference type doesn't create a copy of variable.

stack overflow is avoided...

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Call by reference can reduce memory copy operations and allow multiple return values without using code that impedes portability.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of pass by reference as compared to pass by value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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).


How does java pass primitive types and objects?

Java uses only pass by value. Primitive data types are passed purely as pass by value whereas for objects a value which is the reference to the object is passed. Hence the whole object is not passed but its reference gets passed. All modifications to the object in the method would modify the object in the Heap.


Is call by reference and pass by reference same thing?

Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.


What are the types to pass a structure to functions?

A structure is a type so you just need to pass the structure as you would any other data type: by reference or by value.


What is actually passed to a function when an object is passed to a function through a pointer?

When an object is passed to a function by pointer, a copy of the pointer's value is passed to the function. Pointers are always passed by value, but because the value is a memory address than can be dereferenced, they enable us to pass objects by reference. In languages such as C which have no concept of references, this was the only way to pass by reference. C++ introduced proper references (aliases for existing objects), thus when we want to pass by reference we can choose to use a pointer or an actual reference. Normally we'd only pass by pointer when passing an optional argument that defaults to NULL when no argument is given. Otherwise we pass by reference because references can never be NULL.

Related questions

What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


Differences between pass by value pass by reference in java?

pass by value is the technique where a copy of the variable is passed to the method as argument. This value can be modified inside the method but that would not affect the original value. Pass by reference is the technique where the reference to the actual variable is passed to the method as argument. Any changes to this variable would affect and alter the original value. Usually primitive data types are passed by value and objects are passed by reference in java.


Is array list value type or reference type?

Arrays are reference type. array values are always pass by reference.


What are the possibe problems if you use call by value instead of call by reference?

When you pass by value you essentially pass a temporary copy of the value. If the value's parameter is declared const, then copying the value could be costly, especially if the value is a large and complex structure or object. If the value's parameter is non-const, then it has to be assumed the function intends to alter the value in some way. If you pass by value, only the copy will be affected, not the original value. When a parameter is declared constant, passing by reference is generally the way to go. When it is non-const, pass by reference if you fully expect any changes to be reflected in the original value, otherwise pass by value.


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).


How does java pass primitive types and objects?

Java uses only pass by value. Primitive data types are passed purely as pass by value whereas for objects a value which is the reference to the object is passed. Hence the whole object is not passed but its reference gets passed. All modifications to the object in the method would modify the object in the Heap.


Is call by reference and pass by reference same thing?

Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.


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 the advantages of call by reference?

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address. Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function. In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.


Difference between pass by value and pass by reference?

When you pass by value you pass a copy of the name. That is, the function's parameter is assigned the value of the name you pass. If the function parameter is a class type, then that class' copy constructor is called, automatically, which assigns the name's member value(s) to a new instance of the class. The function parameter is temporary, it has local scope, and will therefore fall from scope when the function returns. Thus any changes made to the local name within the function have no effect on the name you passed -- they are completely separate names. When you pass by reference you pass the name itself. To be precise, you pass the memory address of the name, its reference. This address is assigned to the function's reference parameter. Thus any changes made to the reference parameter's value will change the name you passed, since they both refer to the same memory address. Passing by reference is the most efficient method of passing objects (instances of a class) to functions as there is no overhead in copying the object. And if a reference parameter is declared constant then you can be assured the function will not alter the immutable members of the object (while there are ways around this, if a function really intends to alter a reference parameter then it should not be declared constant in the first place). Pass by value should only be used when the value is a primitive data type (generally equal to or smaller than a pointer), or when the function needs to alter the value but must not alter the name that was passed. Note that pointers are always passed by value, but since they can store a memory address they can be treated just as if they were references (assuming the pointer is non-zero of course). When passing pointers, the pointer and the address being pointed at can both be independently declared constant (that is, either, both, or neither can be constant). To pass a pointer by reference you must pass a pointer-to-pointer type, which is itself passed by value. This allows the function to not only modify the address being pointed at (making it point to another address), but to also modify the value at that address, just as if you'd passed the value by reference.


What the difference between pass by value and pass by reference?

Pass by value calls a function or returns a value based on the number or "value". Maybe you're calling a function with the value of 5, you would just call the function passing the 5. The function can't change the 5, it's always a 5. Pass by reference calls a function and passes a pointer to a memory location that contains the value. You might call the function pointing to a location that contains the number of people who live in your home. That location has been populated with a value, so the function can look at that location and determine that yes, there are 5 members in your family. But the function may be responsible for updating the number of people in your family and you just had a baby, so the function would update the value that is stored at the location. Pass by reference just gives a "pointer" to the memory location. In reality, when a string value is used in any "C" application, it passed by reference.


What are the types to pass a structure to functions?

A structure is a type so you just need to pass the structure as you would any other data type: by reference or by value.