When an object is passed by reference, the function receives direct access to the original object in memory, not a separate copy of it.

Core idea in one line

  • Any change the function makes to that object is visible to the caller, because both refer to the same memory location.

What “passed by reference” really means

Think of a variable as a label pointing to a box in memory:

  • Pass by value : you hand the function a copy of what’s inside the box. The function can change its copy, but your original box stays the same.
  • Pass by reference : you hand the function the address of your box. Both of you now talk about the same box, so any change affects the original.

In technical terms, the parameter in the function becomes an alias for the original variable: they both refer to the same memory location instead of two separate locations.

What actually happens when you pass by reference

When an object is passed by reference:

  1. No new object copy is created for that parameter.
  1. The function can:
    • Modify fields or elements of the object (e.g., change object properties, push to a list, update a map).
 * Observe all those mutations reflected immediately outside the function.
  1. The function cannot avoid affecting the caller’s object if it mutates it, because they share the same underlying data.

Example scenario (language-agnostic idea):

  • You pass a list or object to a function.
  • The function appends an item or changes a field.
  • After the function returns, the caller sees the list/object with the new item or updated field, because they’re looking at the same instance, not a copy.

Important nuance: changing the object vs. changing the reference

Two different operations often get mixed up:

  1. Mutating the object (contents change, same identity):
    • Example: object.x = 42, list.append(5), dict["key"] = "value".
    • With pass by reference, these changes are visible to the caller, because both refer to the same object.
  1. Rebinding the reference (pointing it to a new object):
    • Example: object = new Object(), list = [] inside the function.
    • In a true pass-by-reference language, even rebinding the parameter can change what the caller’s variable points to, because the function holds a reference to the variable itself, not just the object.
 * In many modern languages that “pass a reference by value” (like Java, Python’s “pass by object reference”), mutating the object is visible, but rebinding the parameter only changes the local variable, not the caller’s.

So the key distinction is:

  • “Pass object by reference” → function can change the data in that object for the caller.
  • “Pass variable by reference” (strict sense) → function can even make the caller’s variable point to a completely different object.

Why this matters in real code

Some practical consequences when an object is passed by reference:

  • Side effects:
    • Helper functions that take objects by reference can unexpectedly modify caller state if you are not careful, leading to subtle bugs.
  • Performance:
    • Passing by reference can be more efficient for large objects, because you avoid copying big structures and instead share the same memory.
  • API design:
    • Libraries often use pass-by-reference style parameters when they intend to “fill”, “modify”, or “output” data through parameters rather than only through return values.

A common pattern: “out parameters” or “in/out parameters”, where the function updates an object argument for the caller, such as filling a result struct or accumulating into a list.

Mini FAQ: common viewpoints from discussions

  • “Is pass by reference bad?”
    • Not inherently. It’s powerful but can make code harder to reason about because functions can silently change external state. Many style guides recommend limiting such side effects.
  • “Why do some languages avoid true pass-by-reference?”
    • To keep function calls more predictable and avoid confusion between mutating an object vs. rebinding a variable. Some languages allow mutation via references (like object references) but not rebinding of the caller’s variable.
  • “What’s happening in Python / Java / JavaScript?”
    • They pass a reference value to the function (often called “pass by object reference” or “call by sharing”):
      • You can mutate the shared object and the caller sees the change.
      • Reassigning the parameter name inside the function does not change the caller’s variable.

HTML table: effects of passing an object

html

<table>
  <thead>
    <tr>
      <th>Operation inside function</th>
      <th>True pass-by-reference language</th>
      <th>"Reference value" style (Java/Python-like)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Modify fields/elements of the object</td>
      <td>Caller sees the modifications (same object shared).</td>
      <td>Caller sees the modifications (same object reference passed).</td>
    </tr>
    <tr>
      <td>Reassign parameter to a new object</td>
      <td>Caller’s variable can now point to the new object.</td>
      <td>Only the local parameter changes; caller’s variable still points to the original object.</td>
    </tr>
    <tr>
      <td>Memory usage on call</td>
      <td>No full copy of the object is made; reference is shared.</td>
      <td>No full copy of the object is made; reference value is copied.</td>
    </tr>
    <tr>
      <td>Risk of unintended side effects</td>
      <td>High, for both mutation and rebinding.</td>
      <td>Medium, mainly due to mutation of shared objects.</td>
    </tr>
  </tbody>
</table>

TL;DR

When an object is passed by reference, the function and the caller share the same underlying object , so any in-place modifications inside the function are visible to the caller; in strict pass-by-reference, even changing which object the parameter refers to can update the caller’s variable as well.

Information gathered from public forums or data available on the internet and portrayed here.