In C++ when should you pass by reference or by value?
-
-
Answer:
You pass by value if the value is not to be modified by the function and the value itself may be calculated. So you would tend to pass by value the numbers that go into mathematical functions, for example sqrt(a*a+b*b). You pass by reference when the value is an output or both an input and output from a function, or when the value is very large (in which case a const reference will be used). For example a function that orders two parameters would necessarily need to have them passed by reference. It is not true that classes have to be passed by reference, or though it is normal. Everything is a class, so the fact you can pass somethings by value kind of proves the point. However you do need things like copy constructors to pass by value and when classes get large passing them by value both involves much effort to copy them and much stack space to store them.
Thinker at Yahoo! Answers Visit the source
Other answers
You should pass by reference when you are going to change a variable and by value when you are just going to read or use it in some sort of calculation. It's that simple For example if you have a sum(int , int ) function you can do a cout << sum(a,b); but you can also do a myvar=sum(a,b); You certainly don't have to send the sum back as a separate variable.
You pass by value if the value is not to be modified by the function and the value itself may be calculated. So you would tend to pass by value the numbers that go into mathematical functions, for example sqrt(a*a+b*b). You pass by reference when the value is an output or both an input and output from a function, or when the value is very large (in which case a const reference will be used). For example a function that orders two parameters would necessarily need to have them passed by reference. It is not true that classes have to be passed by reference, or though it is normal. Everything is a class, so the fact you can pass somethings by value kind of proves the point. However you do need things like copy constructors to pass by value and when classes get large passing them by value both involves much effort to copy them and much stack space to store them.
peteams
You should pass by reference when you are going to change a variable and by value when you are just going to read or use it in some sort of calculation. It's that simple For example if you have a sum(int , int ) function you can do a cout << sum(a,b); but you can also do a myvar=sum(a,b); You certainly don't have to send the sum back as a separate variable.
jplatt39
Related Q & A:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to pass ajax value to controller in codeigniter?Best solution by Stack Overflow
- How can I deserialize JSON {name, value} in C#?Best solution by Stack Overflow
- How to pass the value from the javascript to servlet?Best solution by Stack Overflow
- What is happening to my P.C, when it keeps closing out my pages while im in the internet?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.