answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between pass by value and pass by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


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


What is the difference between passing by value and passing by difference?

Passing by value is where you pass the actual value (be it an integer, an array, a struct, etc.) to a function. This means that the value must be copied before the function is called and therefore the value in the caller cannot be modified within the callee. Passing by reference is where a reference/pointer to a value is passed to a function. The reference often takes up less space than copying the actual value (particularly when the value is a struct or an array) and allows you to manipulate the value in the caller within the callee.


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 is a difference between passing by value and passing arguments by reference in visual basic 6 procedures?

When we pass arguments my value, we are passing the value represented in the variable mentioned in the call, and not the variable itself. Therefore, any modifications made to that value will NOT be reflected in the variable mentioned in the call. Pass by reference, as the name suggests passes the reference of the variable mentioned in the procedure call. Any modifications made to the data of that variable is changed at the memory location of that data itself, and will be reflected in the variable itself, after the procedures completes.


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.


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.


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.


Why do you sometimes need to pass arguments to functions by reference?

You should always pass by reference unless there is good reason not to. Passing by reference always passes the object itself whereas passing by value always copies the object. Copying complex objects can severely impact upon performance which is why programmers try very hard to never pass anything larger than the CPU can cope with (typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system). Since a reference is effectively the address of an object and has the same length as a pointer, you can pass by reference either by passing a pointer or by passing an actual reference, depending on the function signature. The difference is that when you pass a pointer, the pointer is actually passed by value and is therefore copied. Also, pointers must also be dereferenced which incurs an extra layer of indirection that you do not incur when passing by reference. When you pass by reference you pass the object itself.


What is difference between 'call by value' and 'call by reference' in the book-computer in programming language-author -vrajaramani?

With call by value semantics, the value of the variable is passed to the function. With call by reference semantics, the memory address of the variable is passed to the function. Pass by reference is typically used to pass large and complex types to functions because there is no performance penalty in passing objects by reference. Passing by reference also makes it possible for the function to modify the argument. If the function does not need to modify the reference, the reference should be declared constant. This assures the caller that the non-mutable members of the object being passed will remain in an unmodified state. Pass by value should only be used to pass primitive or built-in data types. That is, any type that is small enough to fit into a CPU register. This is because pass by value makes a copy of the variable and copying large or complex variables can be an expensive operation in terms of both performance and memory consumption. Imagine passing an array with 1 million elements, each of which must be copy constructed! Copying a single memory address is as cheap as it gets and that's precisely what pass by reference means. Some languages do not support a native reference type and therefore default to pass by value semantics. C is a typical example. However, C supports native pointer types which are simply variables that store memory addresses. Thus you can enable pass by reference semantics by passing a pointer by value. The pointer itself is copied, not the value being pointed at. Many high-level languages do not support native pointers and therefore default to pass by reference semantics. However, this is achieved through resource handles; there is an underlying pointer being passed (by value) behind the scenes but it is (usually) inaccessible to the programmer. Java is a prime example of this. If you wish to use pass by value semantics you must explicitly pass a copy of the object. Defaulting to pass by reference semantics simply encourages efficient coding. C++ is one of a few languages that supports both pointers and references and either method can be used to enable pass by reference semantics. The main differences between a pointer and a reference are that pointers are variables which may be null whereas references are aliases (alternate names for existing objects) which can never be null. More specifically, the address of a reference is the address of the object being referenced and that object must exist (a null reference is an error). A pointer refers to the address stored in the pointer itself (the value of the pointer) thus the address of a pointer is separate from the address of the object being pointed at. This makes it possible for pointers to refer to other pointers, thus making it possible to pass pointers by reference.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).