Passing by Reference vs. Passing by Value

What is the difference between passing a function parameter by reference and passing one by value?
RayRenteriaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

captainkirkCommented:
Passing by value means that your called function receives only the value of whatever parameters you give it, and therefore cannot modify those parameters. Pass-by-reference, however, means that the called function gets pointers or references to those parameters and with those pointers/references can modify the contents of the parameters.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RayRenteriaAuthor Commented:
Thank you!
0
nietodCommented:
That's true, but a little simplistic.

When a parameter is passed by value, the caller CAN modify that value. When a parameter is passed by value, a copy of the parameter is made for the procedure that is called to use.  The modifications are made to thjs copy of the value.  So the changes are not made to the caller's copy of the data.  

When you pass by reference or pointer, no copy is made.  The procedure called has access to the caller's data.  so if the data is modified, the caller "sees" theese changes.

You also might wish to conside the effects of const on things.  Ther are really 4 cases to be dealt with.  that is const/non-const combined with value/reference.

0
captainkirkCommented:
that's true - I intentionally left out the details of the mechanics to keep it simple... the effect is that the caller cannot modify its own copy of the data...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.