A-level Computer Science - Passing by Reference Watch

hallnath1
Badges: 0
Rep:
?
#1
Report Thread starter 2 years ago
#1
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
0
reply
computationchive
Badges: 8
Rep:
?
#2
Report 2 years ago
#2
(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.

0
reply
Jamuk
Badges: 1
Rep:
?
#3
Report 2 years ago
#3
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.
0
reply
username1357713
Badges: 11
Rep:
?
#4
Report 2 years ago
#4
(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.
0
reply
Jamuk
Badges: 1
Rep:
?
#5
Report 2 years ago
#5
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?
0
reply
username1357713
Badges: 11
Rep:
?
#6
Report 2 years ago
#6
(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
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

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.

Personalise

University open days

  • University of the Arts London
    MA Performance Design and Practice Open Day Postgraduate
    Thu, 24 Jan '19
  • Brunel University London
    Undergraduate Experience Days Undergraduate
    Sat, 26 Jan '19
  • SOAS University of London
    Postgraduate Open Day Postgraduate
    Sat, 26 Jan '19

Are you chained to your phone?

Yes (56)
19.31%
Yes, but I'm trying to cut back (117)
40.34%
Nope, not that interesting (117)
40.34%

Watched Threads

View All