Mathwarehouse Logo

Pass By Value vs Pass By Reference

Visualizing the difference
Pass by Value vs Reference Animated Gif

When writing software code, you will spend a lot of time defining, reading and changing variables. Using a variable means you use a descriptive word in your code which holds some information (a number, some text, an object, etc.). This descriptive word is the "title" of the stored information. For example:

						int myAge = 14;
						int month = 1;
						String myName = "Jon";

Variables provice a convenience--you do not have to know where your data is stored in your computer's memory. You define a variable, make your calculations and pass the variable from one function to another, and your operating system automatically deals with where your data is physically located.

However, sometimes it is very useful to know what happens behind variables. Especially in lower-level languages (for example Assembler, or C in some circumstances), it is sometimes even necessary to know where a value is stored in memory. This article will explain how variables are passed around between functions, and specifically explains how it works in Java. In Java, there is a slight but important difference between passing by value and passing by reference.

This article will use a simplified syntax in the code examples, since the theory of passing values is applicable to most programming languages.

What does it mean to "pass a variable"?

The term "passing a variable" is used when a function is called with a variable you defined previously. Let's look at the following example:
						int myAge = 14;

The variable myAge is "passed" to the function calculateBirthYear. The function can then use that variable, for example:

						function calculateBirthYear(int yourAge) {
							return CURRENT_YEAR - yourAge;

There are two ways that variable myAge can be passed to the function. The terms "pass by value" and "pass by reference" are used to describe the two ways that variables can be passed on.

Cliff notes version: : pass by value means the actual value is passed on. Pass by reference means that a number (called a memory address) is passed on, this address defines where the value is stored. Read below for diagrams and examples illustrating this.

How memory works

A basic knowledge of how memory works and how your variables are stored in memory will help to better understand this topic. Since an actual physical memory is hard to draw and different memory types look different (a hard disc vs. RAM for example), it is useful to have an abstract and simple way to imagine how memory looks like.

For most uses (especially for programming beginners), it is enough to understand the concepts. However, once you get experienced in programming, it is very useful to know more about different memory types and memory regions (harddrives, RAM, heap, stack, etc.).

pass by value in memory Diagram 1

Memory can be thought of as blocks, blocks which are next to each other. Each block has a number (the memory address). If you define a variable in your code, the value of the variable will be stored in one of the blocks of memory. Your operating system will automatically decide where the best storage place is.

The gray numbers like 101 and 102 or 106 that are on top of each block show the address of the block in memory, the colored numbers at the bottom show values stored in those memory blocks.

Taking the previous examples again, the variables myAge and month are defined in your code, and they will be stored in memory as shown in diagram 1. The value of myAge is stored at the address 106 and the value of month is stored at the address 113.

There are two main ways that variables can be sent or passed to a function.

Pass by value

Passing by value means that the value of the function parameter is copied (green arrow in Diagram 2) into another location of your memory, and when accessing or modifying the variable within your function, only the copy is accessed/modified and the original value is left untouched. Passing by value is how values are frequently passed in many programming languages like Java.

The value in the memory is copied to another location to be used within the function
copying a variable pass by value Diagram 2

As soon as your software starts processing the calculateBirthYear function, the value myAge is copied to somewhere else in your computers memory. To make this more clear, the variable within the function is named age in this example.

function definition Diagram 3

Anything that you might do to age will not affect the value of myAge.

So, for instance if you changed that function to be birthday(int age) as show below in Diagram 4 and in that function you increase the parameter age, you are in no way changing the original value myAge. As you can see from the picture showing blocks of memory, myAge is stored in memory block 106, but the parameter age is stored in location 152. So when you run line 9 (age = age +1), you are not having any affect on myAge.

function definition Diagram 4

How can you modify variable?

How can code in a function modify variable outside of your function? When passing a variable by value, the only way to update the source variable is by using the return value of the function. This means that only one value can be changed outside of the function.

Example of using return Value

The code below shows how you could return a value to edit a variable's value.

function definition Diagram 5

Memory after updating the variable and running the code in diagram 5.

memory location for returned value Diagram 6

The value of myAge is now 15.

Pass by Reference

Passing by reference means that the memory address of the variable (a pointer to the memory location) is passed to the function. This is unlike passing by value, where the value of a variable is passed on. In the examples, the memory address of myAge is 106. When passing myAge to the function increaseAgeByRef, the variable used within the function (age in this example) still points to the same memory address as the original variable myAge (Hint: the & symbol in front of the function parameter is used in many programming languages to get the reference/pointer of a variable).

memory location for returned value Diagram 7

When calling the function, the value of myAge is changed directly through its reference.

Pass by value vs reference resulting value Diagram 8

Memory after altering the variable through its reference.

The value of myAge is now 15.

Changing Multiple values

With passing by reference, it is now possible to change more than one variable given in the function parameter. In addition, one variable can be returned through the functions return value (often a status, success/fail or the most important variable). For example:

Pass by reference multiple values Diagram 9

Hint: The & symbol returns the address of a variable, the * symbol returns what is stored at the address value, "reversing" the & symbol)

To make a decision whether to use pass by reference or pass by value, there are two simple general rules:

  • If a function should return a single value: use pass by value
  • If a function should return two or more distinct values: use pass by reference

Pass by reference can often be avoided by using arrays or structures (or objects in high-level languages).

Next to CS Animated Gifs