The Student Room Group

A-level Computer Science - Passing by Reference

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 :smile:
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 :smile:


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.

:smile:
Reply 2
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. :smile:
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. :smile:


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.
Reply 4
Yeah I get what you're saying, that probably wasn't a great example :tongue:
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?
Original post by Jamuk
Yeah I get what you're saying, that probably wasn't a great example :tongue:
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 :smile:

Quick Reply

Latest

Trending

Trending