• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Passing by Reference vs. Passing by Value

What is the difference between passing a function parameter by reference and passing one by value?
0
RayRenteria
Asked:
RayRenteria
  • 2
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now