Pass-by-Value vs. Pass-by-Reference: What’s the Difference?

OpenCL vs CUDA

Pass-by-Value vs. Pass-by-Reference: What’s the Difference?

Many novice programmers wonder whether their programming language is a “pass-by-value” or “pass-by-reference” language. To understand which type of language you’re using, you must first understand what those terms mean.

Simply put, pass-by-value languages use a copy of a value stored in memory. Pass-by-reference languages reference the actual value in the memory rather than creating a copy.

Let’s take a look at the differences between pass-by-reference and pass-by-value languages.

Pass-By-Value vs. Pass-By-Reference: Side-By-Side Comparison

Value Storage LocationMemoryMemory
Value Retrieval MethodCopy of Memory ValueReference to Memory Value Location
Example of Language Based on MethodJavaVarious
Other NamesNonePass-By-Address

Pass-By-Value vs. Pass-By-Reference: What’s the Difference?

Now, one crucial thing to remember is that, at this point, pass-by-value and pass-by-reference are considered outdated distinctions. This means you no longer need to worry about whether your functions are a pass-by-value or pass-by-reference. Knowing the difference between them is now esoteric knowledge that won’t get you much use in practical programming applications.

Many languages still in use today were designed to use either pass-by-value or pass-by-reference and may have additional functions to initiate a pass-by-value or pass-by-reference. Still, you no longer need to worry about your chosen function. They’re functionally the same thing now.

Still, knowing the difference between pass-by-value and pass-by-reference can have some uses. So, let’s take a look at the functional differences between them.

Calling Functions

The most significant difference between pass-by-value and pass-by-reference is how they call functions. Calling functions use arguments that the user provides in the code. We refer to the functions called by the arguments to be the “callee,” and the function calling the arguments is the “caller.” The values passed by the caller and callee are known as “actual parameters.” The values received by the function are called “formal parameters.”

When using pass-by-value, a copy of the actual parameters is made in the computer’s memory and then passed to the function as its formal parameters. Any changes made to the formal parameters are seen only by the callee and are invisible to the caller because the caller and callee both have independent copies of the actual parameter’s value. This also means that changes to the formal parameters don’t change the actual parameters in memory.

Pass-by-reference functions will reference the actual value in the computer’s memory. This means that the caller and callee use the same, non-independent value when referencing that location in the memory. This also means that any changes made to the formal parameters will be visible to both the caller and callee since any changes to the formal parameters will be made using the reference to the actual parameters in memory.

When to Use Pass-By-Value

Pass-by-value functions are best used when building a multi-threaded application. The reason that multi-threaded applications best use pass-by-value is that when an application uses pass-by-value, it prevents your application’s other threads from modifying the formal parameters being used by your function. Suppose other threads can modify your formal parameters. In that case, you run the risk of your functions breaking when they receive the wrong parameters. So, passing the information by value allows each thread to use an independent formal parameter, preventing the functions from interfering with each other.

Additionally, passing information by value in a distributed application can save the network overhead and allow it to run more efficiently. You may also need to pass-by-value if you’re using a language like Java which focuses on passing-by-value.

While most modern languages allow for both passing-by-value and passing-by-reference, some languages prefer one over the other. So, you’ll want to ensure that the language you chose supports the function call you wish to use.

Laptops for programming
Depending on what you are building and your information use, pass-by-value or pass-by-reference may be used.

©DC Studio/Shutterstock.com

When to Use Pass-By-Reference

Passing by reference saves the overhead from copying the value; the program doesn’t need to copy the value, and this processing time is saved. Thus, when transferring large amounts of data and structure, it’s far more efficient to use pass-by-reference if you have the option.

You’ll want to use pass-by-reference any time you wish to use the calling function to modify the actual parameters. For example, this method is helpful in applications that need to alter the argument value in the calling function. It’s also more efficient time-wise, so applications that can use the extra time might want to consider passing by reference.

Demonstrating Pass-by-Value Using Java’s swap() and changeValue() Functions

Many novice programmers wonder whether the languages they use primarily use pass-by-value or pass-by-reference. Java is one language that typically uses pass-by-value, if not always.

We’ll start by defining a class for cats and then demonstrate passing-by-value by changing the color of the cat.

public class Cat {
	private String color;
	public Cat() {}
	public Cat(String c) {
		this.color = c;
	public String getColor() {
		return color;
	public void setColor(String color) {
		this.color = color;

We can create a program using the swap() function to change the color of the cat so that the two variables — the one in memory and the copy used by the program — are different.

public class Test {
	public static void main(String[] args) {
		Cat ginger = new Cat("Ginger"); // memory reference = 50
		Cat calico = new Cat("Calico"); // memory reference = 100
		swap(ginger, calico);
		System.out.println("After the swap method executes:");
		System.out.println("`ginger` color value = " + red.getColor());
		System.out.println("`calico` color value = " + blue.getColor());
		System.out.println("After the changeValue method executes:");
		System.out.println("`calico` color value = " + blue.getColor());	
	// Generic swap method
	public static void swap(Object o1, Object o2){
		Object temp = o1;
		o1 = o2;
		o2 = temp;
	private static void changeValue(Cat cat) { // cat = 100
		cat.setColor("ginger"); // cat = 100
		cat = new Cat("tortiseshell"); // balloon = 200
		cat.setColor("calico"); // balloon = 200

Executing this code will output the following:

After the swap method executes:

‘ginger’ color value = Ginger

‘calico’ color value = Calico

After the changeValue method executes:

‘calico’ color value = Ginger

Since the swap() method doesn’t change the original values, only the copies of the values, you end up with two values listed as the same memory location.

This exercise demonstrates that Java typically uses a pass-by-value methodology. However, you can use the changeValue() function to change the values stored in memory.

Demonstrating Pass-By-Reference Using C++’s swapnum()

We can demonstrate passing by reference using C++. The swapnum() function is an example of modifying data passed by reference.

#include <stdio.h>
void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
int main(void) {
  int a = 10;
  int b = 20;
  swapnum(a, b);
  printf("A is %d and B is %d\n", a, b);
  return 0;

Executing this code will result in an output of the following:

A is 20 and B is 10

This code acts upon the location of the data in memory rather than creating a copy of the data. So, when the code swaps the numbers, it causes the data output to change to reflect the new locations of the data within the program, resulting in the values of A and B being swapped at the base level of memory.

Final Thoughts

Passing-by-value and passing-by-reference might be outdated concepts. However, it’s still good to cover your bases and understand the inner machinations of the language you’re using. Whether you’re using a language that primarily uses pass-by-value or pass-by-reference, you’ll be able to access and modify your parameters more quickly if you understand which method you want to use better.

Some languages, like Java, might use pass-by-value exclusively, but others, like C++, may require you to pass-by-reference. So, it makes sense to want to understand these concepts if you plan to learn multiple languages.

Frequently Asked Questions

What is pass-by-value?

Pass-by-value is a method of retrieving information stored in the computer’s memory by copying the data to an independent value.

What is pass-by-reference?

Pass-by-reference is a method of retrieving information stored in the computer’s memory by directly referencing the location of the data in the computer’s memory.

What languages use pass-by-value primarily?

Java is a language that almost exclusively uses pass-by-value when retrieving data from memory.

What languages use pass-by-reference primarily?

C++ primarily passes data from memory to a program by reference.

Is pass-by-value better than pass-by-reference?

In the modern day, these two function types are nearly synonymous and do not require intimate knowledge of their differences.

To top