Um, hello? Is this thing on?

Oh, it is. The topic of the discussion today is on pass by value and pass by reference. There will be time for answering questions at the end, so until then, please be patient. Thank you.

Everyone should know at least one Niklaus Wirth joke - I mean, you wouldn't be here if you weren't slightly interested in the topic, and its a good way to get into the topic.

In Europe, when pronouncing his name, it is pronounced as "Nick-louse Virth". In the United States, his name more closely resembles "Nickel's Worth". Thus, we pass things by value around here.
(wait for laughter - in case of (likely) failure)
Get it? Nickel - a monetary unit, worth, value... ok... never mind.

The choice between passing by value and passing by reference is a choice that programming languages make from the start - do you pass the actual stuff in the object, or do you pass the location (often known as a pointer or sometimes a handle) of the object. At times, each style may be necessary, and it is a real pain to only have one to work with as we found out when dealing with perl version 4.

The primary advantage of passing by value is that you know what you're dealing with - the stuff rather than the location. For most programming languages (especially the C like ones) this is the only way to pass something. This doesn't mean that it is impossible to pass a reference, but the passing of a reference must be done as a value by converting the reference into a value.

Back to the advantage of call by value - when sending the value to a subroutine, it is safe to modify it. If you pass the value of $foo (which is 4) to a subroutine, you can change the value to 5 but the copy is modified - not the original value of $foo. The beauty of this is that when the subroutine exits, changes made to the parameters are 'undone'... or rather, they were never done in the first place.

Passing by value has the disadvantage that if the stuff that is being passed is large, it means a large amount of memory copying about - a bad thing. Picture in perl

@foo = (1 .. 100000)
$answer = sum(@foo);
sub sum
  my $answer = 0;
  foreach my $number (@_)
    { $answer += $number; }
We have now just passed a list of 100,000 numbers as 100,000 numbers. Thats a lot of copying of stuff - especially if the subroutine sum just reads the numbers and sums them up. Wouldn't it be nice to pass just the location of the memory of @foo?

So, now we're at to pass by reference. Very few languages use this method by default, and I can't think of any off the top of my head - yes? Oh, whats that you say? Fortran is purely pass by refrence? Ah, Thank you. So, while very few languages use it by default, the concept of passing something by reference is often used. Going back to that perl example I gave above,

@foo = (1 .. 100000)
$answer = sumref(\@foo);
There, you see it? the \@foo. Now, we have converted the entire list of 100,000 numbers to something that looks a bit more like 'ARRAY(0x80e4aa0)'. Ok, so thats a bit ugly for us, but it is very meaningful to the computer - it is a value that says "there is an array at 0x80e4aa0". Its just a reference to the array. In C, these are called pointers (a pointer to anything can be made by getting the address of the variable with the &varname), and in Pascal the var is used in the parameter declaration to indicate that it is to be passed by reference.

The tricky part with passing by reference (especially in C like languages such as perl and Java) is that you have to dereference the value to use it. In C this is as easy as tossing a * in front of the variable - be careful though not to dereference something that isn't a pointer or is a null pointer, that is a good way to get a segmentation fault. In perl, the the of the object is placed before it to dereference it:

sub sumref
  my ($numbers) = @_;
  my $answer = 0;
  foreach my $number (@$numbers)
    { $answer += $number; }
  return $answer;
See how this is different than the use that I did before? Perl also uses the references to do multi-dimensional arrays (while C does some funky math with the [ ] operator).

One of the things to realize when you've got the reference to something, is that you've really got it and not a copy of it. If you make a change to a reference, that change will be reflected in the original object too. Thus, as a programmer, if you need a reference passed to you for efficency, don't go changing parts of it willy nilly unless you want this to happen, and it is well documented. This documentation is especially important when working on projects with other programmers that may not know what you're doing in your subroutine - you wouldn't want to pass a variable into a subroutine and get it back mangled when you were expecting it to be untouched.

There are a number of other ways of passing things other than by value and by reference - here is a summary of them along with some classic examples.

Thank you, now, any questions?

Yes, you with the /msg?

  • Having your lover passed by reference is good if you want to establish a permanent handle on him/her.
  • Having your lover passed by reference is good if you want to make a difference.
  • Having your lover passed by value is good if you are jealous – you can make sure no one else manipulates him/her.
On the other hand,
  • Having your lover passed by value is good if you want a fling – manipulate him/her as much as you want, it won’t effect his/her state.
  • If you have your lover passed by value no one gets upset if you have him/her garbage-collected.
If your lover is constant, and you want to do everything possible to keep from changing her, you must pass her by value.

However, if your lover is very large, larger than you can easily lift, you will probably prefer to just get her reference, and go to her, rather than bring her to you.

If you are forced to share your lover with others, then you will need to have her passed to you by reference. But be sure to leave a sign on the door to prevent unwanted interruptions.

Also, I learned the hard way today that while it may be useful to pass in a lover by reference, it is a very bad idea to return her by reference (If you have declared her your one and only), as others may sully her before your recipient has a chance to.

Log in or register to write something here or to contact authors.