Of Zen and Computing

The Difference Between Pass-by-Value and Pass-by-Reference

Wednesday, September 17, 2008

The difference between pass-by-value and pass-by-reference is a topic that trips up a lot of people in introductory computer science courses. Understanding this topic is absolutely necessary for anyone who wishes to become a decent programmer. In this brief article, we will give you a straight-to-the-point explanation of the difference between pass-by-value and pass-by-reference.

Pass by value: working with copies

Pass by value: A copy of the value of your variable is made, and that copy is passed to the function you have called.

A function or subroutine that is passed a variable by value is working with a copy of that variable’s contents. The original variable’s contents are not affected by what happens inside of your subroutine. The scope of the copy is restricted to the lifetime of your subroutine. Upon exit, the copy will likely be taken care of by garbage collection unless you explicitly take some sort of action to keep it around, such as returning its value to caller.

/* An example of pass-by-value. */
#include <stdio.h>

void doSomething(int a);

main() {
  int myVar;
  myVar = 1;
  doSomething(myVar);
  printf("%d", myVar);
}

void doSomething(int a) {
  a = 2;
}

The output of the code above is “1″.

Pass by reference: working with originals

Pass by references: A reference to location of the original variable’s contents is passed to the function.

A function that takes a value by reference is given the address of the location in memory where the variable’s value can be found. Any statements in the function then operate on the original variable’s contents. Changes made will persist after your function or subroutine has run its course.

/* An example of pass-by-reference. */
#include <stdio.h>

void doSomething(int *a);

main() {
  int myVar;
  myVar = 1;
  doSomething(&myVar);
  printf("%d", myVar);
}

void doSomething(int *a) {
  *a = 2;
}

The output of the above code is “2″.

By the way

In this article, we have used the C programming language to illustrate the difference between pass-by-value and pass-by-reference. These are concepts however, and are not limited to C programming. The code in this article is present only to give you an example of how a programmer might work with these concepts.

Whatever programming language you happen to be using will have its own syntax for handling references. If you are using a C-style language such as C++ or PHP, the syntax is going to be very similar to what you see here. Other languages’ syntax may be a little different. Check the documentation, and remember that the different code is just an alternate route through the same concept, to the same end result.

Categories: Code

Tags:

Digg icon StumbleUpon icon del.icio.us icon Facebook icon

Other articles related to this page

© 2006-2009 OfZenAndComputing.com
E-mail Disclaimer | Terms of Service & Disclaimer | Sitemap

Subscription Options
Search Our Archive of How-To Articles and Blog Posts