answersLogoWhite

0


Best Answer

Pass By Reference :

In Pass by reference address of the variable is passed to a function. Whatever changes made to the formal parameter will affect to the actual parameters

- Same memory location is used for both variables.(Formal and Actual)-

- it is useful when you required to return more then 1 values

Pass By Value:

- In this method value of the variable is passed. Changes made to formal will not affect the actual parameters.

- Different memory locations will be created for both variables.

- Here there will be temporary variable created in the function stack which does not affect the original variable.

User Avatar

Wiki User

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

Wiki User

14y ago

When you pass a parameter by value, you are creating a copy of the original variable and the receiving (method/function) cannot change the original value. When you pass by reference you are passing a pointer to the original object and therefore any changes are made to the original value.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In .NET:

Both are passed by pointer. However, call by value the actual value (caller value) passed will not change. Call by reference the actual value (caller value) passed will change.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Pass by value and pass by reference refers to the way in which an object is passed into a function, as determined by the function itself. The following examples demonstrate the difference between the two types of function.

void byVal(int i);

void bRef(int& i);

Note that the only difference is that the parameter in byRef() includes the reference operator (&). To understand how these differ, let's define these functions with trace code to examine the properties of the variables they actually work with.

void byVal(int x)

{

cout<

cout<<"x at memory address 0x"<

cout<<" with value "<

}

void byRef(int& x)

{

cout<

cout<<"x at memory address 0x"<

cout<<" with value "<

}

int main()

{

int y=5;

cout<

cout<<"y at memory address 0x"<

cout<<" with value "<

byVal(y);

byRef(y);

return(0);

}

Output:

main(): y at memory address 0x0020FD04 with value 5

byVal(x): x at memory address 0x0020FC18 with value 5

byRef(x): x at memory address 0x0020FD04 with value 5

Note that while the memory addresses may vary each time you run this program, the memory address of y in main() is always the same as the memory address of x in byRef(), while the memory address of x in byVal() is always different. You might ask how it is possible for two variables to reside at the exact the same memory address, but the answer is that there is only one variable, y, and the x in byRef() simply refers to this variable. That is, a reference is simply an alias for a variable, an alternate name.

Passing by reference is the preferred method of passing complex objects to functions. To understand why, let's look at a more complex example where the variable is an user-defined object. We'll also include some trace code within the object's constructors so we can see exactly what goes on Behind the Scenes.

#include

#include

using namespace std;

class obj

{

public:

obj(int data):m_data(data) { cout<

cout<<" with value "<

return( os );

}

void byVal(obj x)

{

cout<

cout<<"x"<

}

void byRef(obj& x)

{

cout<

cout<<"x"<

}

int main()

{

obj y=5;

cout<

byVal(y);

byRef(y);

return(0);

}

Output:

  1. obj(): m_data=5
  2. main(): y at memory address 0x001FF98C with value 5
  3. obj(obj): m_data=5
  4. byVal(x): x at memory address 0x001FF894 with value 5
  5. byRef(x): x at memory address 0x001FF98C with value 5

Note that line numbers have been included in the output for reference.

Line 1 of the output corresponds with the first line in main(), where y is constructed and initialised with the value 5 (the output line was emitted by the default constructor).

Line 2 corresponds with the second line in main().

Line 3 is a trace from the object's copy constructor. The copy constructor was invoked automatically the moment we called byVal, which then assigned that copy to x.

Line 4 is a trace from byVal(), showing that x resides at a different address to that of y.

Line 5 is the trace from byRef(), showing that x is the same instance as y.

You will note that line 3 demonstrates why passing objects by value is so inefficient. Imagine if y were a highly complex object, such as a polymorphic object containing multiple embedded objects and derived from multiple base classes. Every time you pass that object to a function by value you create a copy of that object, and every object encapsulated by the class, including its base classes and its derivatives, also has to be copied as part of the construction process. This is detrimental to both performance and memory consumption.

While it may occasionally be necessary to pass a copy of an object into a function in order to prevent the function from changing the original object, this can easily be catered for by providing two overloads for every function: one that accepts a reference, and another that accepts a value. The by value overload then simply passes its copy to the by reference overload.

But when a function does not need to alter any of the object's immutable members, a single by reference function is all that is required. Moreover, you can re-enforce the constant nature of the object by declaring the parameter as a constant reference:

void byRef(const& obj);

The use of the const keyword has two purpose: primarily to alert consumers to the fact the object's immutable members will not be altered in any way by the function (only constant member functions will be accessible to the function), but also to ensure that you yourself do not inadvertently alter the object should you later decide to modify the implementation of the function. In other words you enlist the compiler to prevent you from doing things you shouldn't be doing, thus ensuring your code remains robust.

You will note that passing by reference is effectively the same as passing a pointer to your object. However, you should always use references rather than pointers when passing objects to functions, unless the object is an optional parameter. In this case, you can default the pointer parameter to NULL which can't be done with a reference (a NULL reference completely invalidates your program). In other words, a reference must always refer to an existing object, whereas a pointer can refer to NULL. Thus if an object must exist for a function to work, use pass a reference, otherwise use pass by pointer.

Note also that pointers are always passed by value. In order to treat a pointer by reference, you must pass an indirect pointer instead (such as a pointer to a pointer). This then allows the function to not only alter the object being pointed at (assuming the pointer type is not constant), but also to change which object it points at (assuming the pointer itself is not constant).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

first you need to understand the formal and actual parameter.An actual parameter is the parameter passed to a function while calling it, a formal parameter is a parameter to which the values passed from the calling function is assigned.

When a function is called and a copy of values of actual parameters is passed to the called function, it's called as call by value.

When the address of actual parameter is passed to the called function, it's called as call by reference.

e.g.

if int a,b; is a declaration in a calling function and a function call to a function having prototype, int add(int, int) is made i.e.

add(a,b) ;

then it's call by value.

And if function call to a function having prototype, voidadd(int* , int* ) is made i.e.

add(&a,&b) ;

then it's call by reference.

Answer:

There is only call-by-value in C; you can emulate call-by-reference using pointers.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Pass by value copies the value whereas pass by reference passes the actual value. As a result, pass by reference is quicker. If a function does not need to alter the value being passed, then there's little point in passing it by value. A constant reference is a far better option. If the function does alter the value, and the changes are expected, a non-constant reference is the ideal choice. If changes are not expected, then either pass by value, or copy the value and then pass the copy by reference.

Pointers are always passed by value (only the pointer is copied, not the object being pointed at), but they act like references. However, pointers should only be passed if there's ever a possibility of passing a pointer to NULL. If an object is guaranteed to exist, passing by reference is the method of choice.

Passing by constant value is a pointless endeavour since the value is copied, and the copy will fall from scope when the function returns, and therefore has no effect on the actual parameter passed by the caller. However, if the value must remain constant within the function, declaring the formal parameter as a constant is no bad thing.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

When you pass by value you pass a copy of the value. When you pass by reference, you pass the address of the value.

void foo(int x){x*=2;} // pass by value

void bar(int &x){x*=2} // pass by reference

int main()

{

int y=1;

foo(y);

bar(y);

return( 0 );

}

In the above example, calling foo(y) passes the value of y (1) which is then assigned to x in the function. x and y are separate variables so although foo() changes the value of x, this has no effect on y which is still 1 when the function returns.

Calling bar(y), on the other hand, passes the address of y and assigns that address to the reference x. Both x and y refer to the same memory so when bar() changes the value of x it changes the value of y, thus y will be 2 when the function returns.

Note that the only difference with pass by reference is that the parameter x has an ampersand to denote that it is a reference to the argument (addressof) rather than a local copy of the argument's value.

Pass by reference is the preferred method of passing all complex objects to functions as there is no need to copy the object. Failure to do so will result in an often unnecessary overhead in calling the object's copy constructor. Where the function does not need to alter the object, the reference should be declared const. If it is non-const, the caller can fully expect any changes to be reflected in the original object. The only time a function should ever accept complex objects by value is when it must change the object but must not change the object that was passed. Passing by value ensures the function only works on a copy of the object.

Note that pass by reference is not unlike pass by pointer. The only difference is that pointers must always be passed by value. To mimic passing a pointer by reference, you must pass a pointer to pointer instead.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the differences between call by value and call by reference in c plus plus?
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).


What does c plus plus use call by value or call by reference?

When we call a function in C++ by passing the values as arguments, it is called call by value. e.g #include&lt;iostream.h&gt; #include&lt;conio.h&gt; int add(int,int); int main() { int a,b,c; cout&lt;&lt;"Enter numbers."; cin&gt;&gt;a&gt;&gt;b; c=add(a,b); cout&lt;&lt;"Sum : "&lt;&lt;c; return 0; } int add(int a,int b) { int c; c=a+b; return c; }


When is it better to call by value vs. call by reference?

Call by value only when a function must modify a value, but you do not want those changes to be reflected in the original value. Passing by value creates a copy of the value which falls from scope when the function returns. Note that in C++, call by value is the default. This is fine for all primitive data types, including pointer variables, but for complex objects you will generally want to pass by reference to avoid as much unnecessary copying as possible. Use the const keyword to enlist the compiler's help to ensure immutable members are not changed during a function call. Non-const references imply the function will alter the immutable members. That's fine when the changes are expected, but it's a good idea to provide a pass by value overload to cater for automatic copying whenever those changes are not wanted. You can also copy the object yourself and pass it by reference, but the copy remains in scope after the function call returns.


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 is calling by reference How it is different from call by value?

Call By Value, the standard way of doing things in C and C++, is where a copy of an object is placed in the parameter stack. The called function can access and manipulate that copy at will, but it cannot change the original copy because it has no way of knowing where that original copy is located. Call By Reference, on the other hand, is where the address of an object is placed in the parameter stack. Using extra syntax, the * or the -&gt;, the called function can access and manipulate the original copy at will.

Related questions

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 differences between function call by value and function call by reference?

Well let me start by saying that passing by value is like making a copy of something. The copy my be changed or destroyed without changing the original. Example: void PassByValueFunction (int); main() { int num = 5; cout


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

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


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


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.


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


What did Marx call me differences between what the workers produce and what they earn?

Marx referred to the difference between what workers produce and what they are paid as &quot;surplus value.&quot; This surplus value is captured by the capitalist as profit, leading to exploitation of the workers according to Marx's theory of surplus labor.


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.