The Student Room Group
Reply 1
Passing by value allows data that is passed to be edited within the subprogram/module and this edited data to be passed back to the main program
passing by reference allows the data to be used and edited, but this edited data will only be valid within the subprogram, ie any changes will not be passed back to the main program
Reply 2
roar558
Passing by value allows data that is passed to be edited within the subprogram/module and this edited data to be passed back to the main program
passing by reference allows the data to be used and edited, but this edited data will only be valid within the subprogram, ie any changes will not be passed back to the main program


Thanks, also what might be an example of each in use?
Reply 3
Well i suppose if you are trying to get inputs using a module for example you would pass by value

If you wanted to display a set of results using a module, you would pass by reference

However if an array is used, it will display an error message if you try to pass by reference, it only works when you pass by value
Reply 4
roar558
Passing by value allows data that is passed to be edited within the subprogram/module and this edited data to be passed back to the main program
passing by reference allows the data to be used and edited, but this edited data will only be valid within the subprogram, ie any changes will not be passed back to the main program


You got these the wrong way round. Pass-by-reference means just that, you pass a reference to the memory location of the variable in question. Pass-by-value creates a new variable of the same value. Hence, assignments to pass-by-reference variables (since they are just pointers to what they are "referring" to) will change the original variable.

However if an array is used, it will display an error message if you try to pass by reference, it only works when you pass by value


This is not true either; passing by reference is much more efficient than passing by value, especially for arrays. Consider the fact that an array is just a series of contiguous memory locations - is it quicker (and more spatially sound) to duplicate the whole array or just to pass the location of the first element and go from there?

Latest

Trending

Trending