answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is it better to call by value vs. call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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


What is safety as value choice vs compliance?

Many companies feel that safety as a value choice is very important. Safety as a value choice is a protection of oneself. However, if safety is followed than compliance is sure to follow.


Which cicuits light the bulb better-Bulbs light better in which kind of circuits?

If you are comparing parallel vs series circuits, light bulbs produce more light in parallel circuits.


Why do you need to create an object reference variable to call methods cant you directly use the objects that is created?

Creating an object and invoking a method of that object are 2 distinct steps. From good programming practice, it is better off to have 2 separate statements at least to do this. For example: (new object()).ToString(); // creates an instance and invoke ToString() vs. object objRefence = new object(); objRefence.ToString(); The variable objReference is needed to invoke the method ToString(), because the object refereced of the one being created from the new operator needed to be cached somewhere. There is no major differences between the 2 above. However, if there is another method to be invoked, the second way (use variable reference) is the only way, the first one cannot. (You can replace ToString() with GetHashCode(), and prints it on the console twice, the first one may produce 2 different values, while the second one will be the same value. Think of GetHashCode() returns the object id in a way)


Difference between a natural aspirated and turbocharged engine in terms of a Pressure vs Volume diagram?

Naturally aspirated engine are simply better than a smaller turbo charged engine. It has greater torque and no need for maintenance. You also have better control of the car while driving-(racing)

Related questions

Which is better call of duty or mx vs ATV?

there is no way to put it but to say that mx vs atv s far far far far far far better.


Which is the better value 1 liter for 23.00 vs 1.75 liter at 34.00?

1.75 at 34.00


Call of duty modern warfare 2 skar-h vs Acr?

scar is better because it has better accuracy and damage and rate


Is smackdown vs raw 2010 better than cod?

it is probably better than call of duty waw or cod 4 but not mw2


What is beauty vs brain?

Depends on the context. It is generally a reference relating to preferential hiring of women who are very attractive physically -vs- women who are perhaps less attractive but more qualified because of a better education, perhaps.


Cell references vs values?

Cell references are the addresses of cells and values are what are contained in the cell. So A3 could be a cell reference and the number 42 could be a value in the cell.


What is better Marvel vs Capcom 2 or Marvel vs Capcom 3?

In my opinion, Marvel vs Capcom 3 is better.


Is call of duty modern warfare 2 better than alien vs predator 3?

No Modern warfare 2 is a terrible game full of cheaters. Alien vs Predator isn't that good either


Is silver or gold better?

gold. its worth more money silver, It's much cheaper than gold (estimated $35.00 vs. $1,700.00) also Silver value jumped faster than gold too (estimated 400% vs. 300% increase in value in 10 years)


Was there a title Jason vs Tina?

To my knowledge, unless it is in reference to a non-horror Jason, then no.


Which game is better FIFA 11 or Smackdown vs Raw 2011?

smackdown vs raw 2011 is better then fifa 11


Should you write a poem about strawberries vs watermelon or strawberries vs canteloupe or canteloupe vs watermelon?

strawberries vs cherries are better