Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    Hey guys,
    Just a quick query. I hope someone could explain for me the difference bypassing by reference and passing by value when dealing with subroutines.

    Thanks in advance
    Offline

    8
    ReputationRep:
    (Original post by hallnath1)
    Hey guys,
    Just a quick query. I hope someone could explain for me the difference bypassing by reference and passing by value when dealing with subroutines.

    Thanks in advance
    Hi,

    Basically, 'by ref' allows you to change the value of the variable (that has been passed) whereas 'by val' only passes the 'value' of the variable and so does not allow you to change the value of the original variable.

    Offline

    1
    ReputationRep:
    Just to add to the previous response a little:

    Say I had a method void SaveFile(string FileLocation) which saved a file to some specified file location.

    Passing by value:
    I could do SaveFile(MyDocuments/Holiday_Photos/Ibiza_2004). In this case I am specifying an exact parameter and passing it to the method.

    Passing by reference:
    I could do SaveFile(photoFolderPath) where photoFolderPath is some previously assigned variable. The program would work out where to actually save it then instead of you having to hard code it.

    In reality I don't think many things are typically passed by value (at least not in high-level languages maybe).
    Hope that helps.
    Offline

    11
    ReputationRep:
    (Original post by Jamuk)
    Just to add to the previous response a little:

    Say I had a method void SaveFile(string FileLocation) which saved a file to some specified file location.

    Passing by value:
    I could do SaveFile(MyDocuments/Holiday_Photos/Ibiza_2004). In this case I am specifying an exact parameter and passing it to the method.

    Passing by reference:
    I could do SaveFile(photoFolderPath) where photoFolderPath is some previously assigned variable. The program would work out where to actually save it then instead of you having to hard code it.

    In reality I don't think many things are typically passed by value (at least not in high-level languages maybe).
    Hope that helps.
    I feel like your example is kind of misleading personally. When you pass by value you can program SaveFile(photoFolderPath). If you were passing by value you would only pass the value of that variable though. If you were passing by reference you would kind of pass the memory location of the variable so that changes to the variable inside the subroutine will have an effect outside of the subroutine, while by value they would only effect inside the subroutine. Neither have to use hard-coded variables/values.
    Offline

    1
    ReputationRep:
    Yeah I get what you're saying, that probably wasn't a great example
    Isn't it basically that with PBV you end up with effectively another instance of that variable that exists within the scope of the method, whereas PBR you can modify the original variable?
    Offline

    11
    ReputationRep:
    (Original post by Jamuk)
    Yeah I get what you're saying, that probably wasn't a great example
    Isn't it basically that with PBV you end up with effectively another instance of that variable that exists within the scope of the method, whereas PBR you can modify the original variable?
    Pretty much yeah
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    What's your favourite Christmas sweets?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.