answersLogoWhite

0


Best Answer

In a pass by pointer (*, the original C standard), you're not passing the actual variable but rather its address in memory. This is required because in standard C, all function parameters are passed by value and therefore disappear when the function call is completed. In order to access and alter the original variable, you have to dereference the pointer using the address provided in the pointer argument, and this must be done each time the original variable is altered. Keeping this in mind requires vigilance to be sure you know which object referring: the variable or its memory address.

In a pass by reference (&, introduced in C++), you're telling the compiler that the variable being passed into a function has to be fundamentally linked to the original. The compiler takes care of the dirty work and lets you refer to the variable with simple assignment statements--no pointer handling required.

User Avatar

Wiki User

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

Wiki User

11y ago

Consider the code that follows. In the main() function we declare an integer, num, and initialise it to the value 100. We then call three separate functions which accept numas an argument, first by value, then by reference, and finally by pointer. In each case we double the value of the parameter to show the effect it has upon numitself.

Note how the PassByValue function makes a copy of num. We know this because the parameter val has a completely different memory address to that of num. This means that anything we do to val will not affect num.

However PassByRef passes num directly because ref is a reference to num (they both refer to the same memory address). Thus anything we do to ref we actually do to num. Thus num is doubled when we double ref.

Finally, PassByPtr proves that pointers are just variables that are passed by value, because ptr has its own memory address allocated to it. The value stored in this memory address is the memory address of num. Dereferencing this memory address shows us the value of num. Thus doubling the dereferenced value of ptr doubles the value of num.

#include

void PassByVal( int val )

{

printf( "PassByVal(int val)\taddress of val: 0x%.8x, value of val: %d\n" , &val, val );

val *= 2;

}

void PassByRef( int & ref )

{

printf( "PassByRef(int & ref)\taddress of ref: 0x%.8x, value of ref: %d\n" , &ref, ref );

ref *= 2;

}

void PassByPtr( int * ptr )

{

printf( "PassByPtr(int * ptr)\taddress of ptr: 0x%.8x, value of ptr: 0x%.8x, dereferenced value of ptr: %d\n" , &ptr, ptr, *ptr );

*ptr *= 2;

}

int main()

{

int num = 100;

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num, );

PassByVal( num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

PassByRef( num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

PassByPtr( &num );

printf( "main()\t\t\taddress of num: 0x%.8x, value of num: %d\n" , &num, num );

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

In call by value, you can only pass values stored in variables and catch them in other variables, actual variables are mostly unaffected (value of only one variable can be modified in one call). In call by reference, you pass address of variables, so actual variables are modified (multiple values can be modified in one call).

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

C++ only supports the call by value semantic. When we pass an object to a function, we do not pass the object we pass the object's value. To achieve this, the object must be copied. Thus the function operates upon a copy of the object, never the object itself.

Pass by reference applies when the object being passed is a memory address. The address is passed by value just like any other object, however when an address refers to an object, we are actually passing a reference to that object. Addresses can be passed by reference or by pointer (preferably constant reference or constant pointer). We use pointers when "no object" is a valid argument, otherwise we use references.

Copying objects can be expensive (depending on their size, in bytes), but copying an object's address has minimal cost and can be done in constant time. Thus we can pass objects of any size to functions much more efficiently by passing their addresses rather than their values.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What the difference between call by reference and call by value return in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between using a return value and a pass-by-reference parameter?

The main difference is that return values can be used in compound statements (such as assignments), whereas output parameters cannot. An output parameter is a non-const parameter that is passed by reference or as a pointer variable.As a general rule, every function should return something through the return value, even if only an error level where zero would typically indicate success (but not always). A function that does not return anything (returns void) is not strictly a function, it is better described as a procedure. However, if a function can be guaranteed never to fail (provides trivial functionality) and has no need to return any other value to the caller, then returning void is a logical option. Just because a function should return a value doesn't mean that it must return a value.However, the return value is limited by the fact that it only permits one value to be returned by a function. Output parameters allow a function to return several values at once and can be used in conjunction with the return value. If there is no need for the return value, it should be used to indicate the error level of the function wherever it would be appropriate. However it is not unusual for a function to accept a reference as an output parameter and to also return that same reference via the return value.Note that although you could return multiple values via a struct or class type, this should only be considered if you have several functions that can make use of the struct or class type. However, in most cases it would be simpler to make those functions members of the struct or class itself.


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.


CAN Overloaded functions can return default values?

I'm not sure I understand you as it wouldn't make sense for a function to return a default value. Do you actually mean can a function return an argument that has a default value? If so, then yes. Any argument passed to a function, whether defaulted or not, can be returned by the same function. If the argument is passed by value then you must return it by value. If passed by reference (which cannot be defaulted) then you can either return by reference or by value. However, if you pass by non-constant reference then you can just use the reference as an output argument, and use the actual return value for some other purpose, such as reporting any error condition(s) created by the function. Overloaded functions are no different to ordinary functions, the only criteria is that each overload has an unique signature. The return value does not form any part of the signature, thus signatures cannot differ by return type alone.


Difference between value semantic and reference semantic?

Typically if one sets a variable like int x = 100; it is value semantics, But if one creates an array, like int[] list = new list[10]; it is referring the location, so it is reference semantics.


Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.

Related questions

What is the difference between value type parameters and reference type parameters?

When a variable is passed by value, the function receives a copy of the variable. When a variable is passed by reference, the function receives a reference, or pointer, to the original data.


What is the difference between using a return value and a pass-by-reference parameter?

The main difference is that return values can be used in compound statements (such as assignments), whereas output parameters cannot. An output parameter is a non-const parameter that is passed by reference or as a pointer variable.As a general rule, every function should return something through the return value, even if only an error level where zero would typically indicate success (but not always). A function that does not return anything (returns void) is not strictly a function, it is better described as a procedure. However, if a function can be guaranteed never to fail (provides trivial functionality) and has no need to return any other value to the caller, then returning void is a logical option. Just because a function should return a value doesn't mean that it must return a value.However, the return value is limited by the fact that it only permits one value to be returned by a function. Output parameters allow a function to return several values at once and can be used in conjunction with the return value. If there is no need for the return value, it should be used to indicate the error level of the function wherever it would be appropriate. However it is not unusual for a function to accept a reference as an output parameter and to also return that same reference via the return value.Note that although you could return multiple values via a struct or class type, this should only be considered if you have several functions that can make use of the struct or class type. However, in most cases it would be simpler to make those functions members of the struct or class itself.


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


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.


CAN Overloaded functions can return default values?

I'm not sure I understand you as it wouldn't make sense for a function to return a default value. Do you actually mean can a function return an argument that has a default value? If so, then yes. Any argument passed to a function, whether defaulted or not, can be returned by the same function. If the argument is passed by value then you must return it by value. If passed by reference (which cannot be defaulted) then you can either return by reference or by value. However, if you pass by non-constant reference then you can just use the reference as an output argument, and use the actual return value for some other purpose, such as reporting any error condition(s) created by the function. Overloaded functions are no different to ordinary functions, the only criteria is that each overload has an unique signature. The return value does not form any part of the signature, thus signatures cannot differ by return type alone.


Difference between reference dimension and basic dimension?

Basic dimension is the numerical value defining the theoretically exact size of a feature. Reference dimension is the numerical value enclosed in parentheses provided for information only and is not used in the fabrication of the part.


What is the difference between place value and face value?

place value is the place of the number in reference to the decimal point, either to the right or left by how many places. face value refers to the number diregarding the positive or negative


What the difference between actual value and earned value?

The difference between the Actual Value & Earned Value is the Project Cost Variance


What is the difference between the place value and face value of 9 in 309812?

the DIFFERENCE between the place value and the face value is 991


Difference between value semantic and reference semantic?

Typically if one sets a variable like int x = 100; it is value semantics, But if one creates an array, like int[] list = new list[10]; it is referring the location, so it is reference semantics.


Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.