SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    In What Situations Would You 'Pass By Value' and 'Pass By Reference'

    Hi All.

    I have been trying to grasp the whole 'Pass By Value/Reference' thing for a few hours now. From what i can make out:

    Passing by value will make a copy of the value, pass it as a function argument and the function will store the changes, the original value is not affected. So for example:
    Code JavaScript:
    var cost = 145;
    var postage = .3;
    var a = function() {return cost + postage;} //Using an Anonymous function for this

    If you was then to pass the value of 'a' to a function argument it would copy and not change the original value.

    If you pass by reference it will change the original value. When you pass a reference to a value through a series of different functions and the value is constantly being changed the change happens on the original value and can be seen outside the function.

    The problem is i cannot think of how or why this would be used. I havn't got to develop many large applications so im trying to think of a few situations when passing by reference would be used.

    A shopping cart maybe? When the final price needs to be updated?

    Throw some examples at me

    I have 2 good JS + DOM books, David Flanagan's and DOM Scripting from Friendsofed, but neither (at least i havn't read that far into the books) tell me much about passing by value or reference.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To start with JavaScript doesn't give you control over this as all primitive variables are always passed by value and all objects are always passed by reference.

    The primary difference between pass by value and pass by reference is that a function that receives all of its data passed by reference is able to do whatever the function needs to do with that data knowing that it will not have any side effects on anything outside of the function. Whatever changes the function makes that need to be passed back to the calling code needs to be 'return'ed. When pass by value is used the function has access to update the values passed to it and hence can have unintended effects on other parts of the code if a field that was passed by value is updated within the function unintentionally (or where you thought it was passed by reference).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The primary difference between pass by value and pass by reference is that a function that receives all of its data passed by reference is able to do whatever the function needs to do with that data knowing that it will not have any side effects on anything outside of the function. Whatever changes the function makes that need to be passed back to the calling code needs to be 'return'ed. When pass by value is used the function has access to update the values passed to it and hence can have unintended effects on other parts of the code if a field that was passed by value is updated within the function unintentionally (or where you thought it was passed by reference).
    Sounds back to front to me.
    Tab-indentation is a crime against humanity.

  4. #4
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Logic_Ali said, is the other way around.

    Passing by value means that the variable inside the function is a completely different variable (even with a different scope) and has only its initial value in common with the original one. It can be changed as will, because is really another variable.

    Passing by reference means that the variable inside the function will actually be a reference to the value of the original variable and any change done either in the function called or the caller will reflect in the other immediately.

    (Of course, in a program running synchronously only the called function will be able to change it when active but if you are running different threads...)
    Guilherme Zühlke O'Connor
    www.z-oc.com

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    To start with JavaScript doesn't give you control over this as all primitive variables are always passed by value and all objects are always passed by reference.

    The primary difference between pass by value and pass by reference is that a function that receives all of its data passed by reference is able to do whatever the function needs to do with that data knowing that it will not have any side effects on anything outside of the function. Whatever changes the function makes that need to be passed back to the calling code needs to be 'return'ed. When pass by value is used the function has access to update the values passed to it and hence can have unintended effects on other parts of the code if a field that was passed by value is updated within the function unintentionally (or where you thought it was passed by reference).
    Yeah that is back to front. If you pass by value the value cannot be changed outside the function. I know that much. Passing by value isn't a problem, its passing by reference that im having a hard time dealing with.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Okay, I somehow swapped the two when I was writing my explanation and gave each the opposite explanation to what I had intended.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •