{"post":{"slug":"pass-by-value-vs-pass-by-reference-2026","title":"Pass by Value vs Pass by Reference 2026: What Really Happens","excerpt":"Pass by value copies the argument; pass by reference shares it. Here is the 2026 guide to what each means, why most languages are neither purely one nor the other, and how to avoid the classic bugs.","category":"code","date":"June 30, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">P</span>ass by value versus pass by reference is where a huge share of \"why did my variable change?\" bugs come from. The concept is simple in theory: pass by value gives the function a copy of the argument, so changes stay local; pass by reference gives the function the caller's actual variable, so changes leak back out. The trouble is that most popular languages do neither in the pure form people expect, and the mismatch between the mental model and reality is exactly where mutation bugs hide.\n\n## What changed in 2026\n\n- **Immutability-by-default kept gaining ground.** More languages and libraries default to immutable data structures, which sidesteps the whole class of accidental-mutation bugs.\n- **Linters flag risky mutation.** Static analysis tools increasingly warn when a function mutates a shared argument, catching the classic mistake before review.\n- **Value types got more attention.** Several languages expanded first-class value (copy) types so developers can opt into copy semantics deliberately.\n- **The vocabulary settled.** \"Call by sharing\" is now the commonly taught term for what most object-oriented languages actually do, reducing decades of confusion.\n\n## What the two terms really mean\n\n**Pass by value** means the function receives a copy of the argument. Whatever it does to its parameter, the caller's original is untouched.\n\n**Pass by reference** means the function receives a reference to the caller's variable itself. If the function reassigns the parameter, the caller sees the new value.\n\n```\n// true pass by reference (e.g. C++ &)\nvoid reset(int& x) { x = 0; }\nint a = 5; reset(a); // a is now 0\n```\n\nThe clean test for true pass by reference: can the function *reassign* the caller's variable and have it stick? In genuinely pass-by-reference languages, yes. In most mainstream languages, no.\n\n## Why most languages confuse everyone\n\nLanguages like the popular scripting and JVM-family ones pass everything by value — but for objects, the *value being copied is a reference*. This is called **call by sharing**. The function gets its own copy of the reference, both pointing at the same underlying object.\n\nThe consequence trips up nearly everyone:\n\n```\nfunction mutate(list) { list.push(4); }   // affects caller: shared object\nfunction reassign(list) { list = [9]; }   // does NOT affect caller: local copy of the reference\n```\n\n`mutate` changes the shared object, so the caller sees `4` added. `reassign` only rebinds the local copy of the reference, so the caller's variable is unchanged. Mutation leaks out; reassignment does not. Understanding this one distinction eliminates most of the confusion — and it is closely tied to how shared mutable state causes [race conditions](/blog/race-conditions-explained-2026) in concurrent code.\n\n## The three behaviors compared\n\n| Behavior | Copies | Reassignment visible? | Mutation visible? |\n| --- | --- | --- | --- |\n| Pass by value (primitive) | The value | No | N/A |\n| Call by sharing (object) | The reference | No | Yes |\n| Pass by reference (true) | Nothing, aliases the variable | Yes | Yes |\n\nThe middle row is what most developers actually work with daily, and it is neither of the textbook extremes.\n\n## How to avoid the classic bugs\n\nKnow exactly what your language copies for primitives versus objects. When a function should not change its input, either work on a copy inside the function or, better, use immutable data structures so mutation is impossible by construction.\n\nPrefer returning new values over mutating arguments. A function that takes data and returns a transformed copy is easier to reason about and test than one that quietly edits what it was given. When you genuinely need to mutate a shared structure for performance, make that intent explicit in the name and documentation so callers are not surprised.\n\n## Pitfalls\n\n- **Assuming objects are copied.** They usually are not; you share the underlying object.\n- **Expecting reassignment inside a function to stick.** In call-by-sharing languages it does not.\n- **Mutating a caller's list or map by accident.** The most common real-world instance of this bug.\n- **Deep vs shallow copy confusion.** Copying the outer object may still share nested objects.\n\n## FAQ\n\n**Is my language pass by value or pass by reference?**\nMost mainstream languages are strictly pass by value, but for objects the value copied is a reference (call by sharing). Truly pass-by-reference languages let a function reassign the caller's variable.\n\n**Why did my array change after a function call?**\nBecause the function received a copy of the reference to the same array and mutated it. The object is shared even though the variable was passed by value.\n\n**What is call by sharing?**\nThe behavior where a function gets its own copy of a reference to the same object. Mutations to the object are visible to the caller; reassigning the parameter is not.\n\n**How do I stop a function from changing my data?**\nPass a copy, or use immutable data structures. Returning new values instead of mutating arguments is the most reliable pattern.\n\n## Where to go next\n\n- [Static vs dynamic typing explained in 2026](/blog/static-vs-dynamic-typing-2026)\n- [Race conditions explained in 2026](/blog/race-conditions-explained-2026)\n- [Recursion vs iteration explained in 2026](/blog/recursion-vs-iteration-2026)","hue":305,"metaTitle":"Pass by Value vs Pass by Reference 2026","metaDescription":"Pass by value vs pass by reference explained for 2026 — what each means, why most languages pass references by value, mutation vs reassignment, and pitfalls.","keywords":["pass by value vs pass by reference 2026","difference between pass by value and reference","what is pass by value","pass by reference explained","call by sharing","mutable vs immutable arguments","reference vs value semantics","why objects change in functions","pass by value 2026","function argument passing"],"tldr":["**Pass by value copies the argument** — changes inside the function do not affect the caller variable.","**Pass by reference shares the same variable** — reassignment inside the function is visible outside.","**Most popular languages pass references by value** — you can mutate the shared object but cannot reassign the callers variable.","**The confusion is about objects, not the mechanism** — mutating a shared object is not the same as pass by reference.","**Skip:** assuming your language does one or the other. Learn exactly what it copies, or you will ship a mutation bug."],"readTime":"4 min read","wordCount":860},"related":[{"slug":"what-is-a-function-2026","title":"What is a function in programming in 2026? A clear guide","excerpt":"A function is a named, reusable block of code that takes inputs and returns a result. This guide explains how functions work, why they matter, and how to write a good one.","category":"code","date":"June 1, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":142},{"slug":"what-is-an-argument-2026","title":"What Is an Argument in 2026? Plain Programming Guide","excerpt":"A clear explanation of what an argument is in programming in 2026, how it passes data into a function, and how it differs from a parameter.","category":"code","date":"May 13, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":212},{"slug":"what-is-a-parameter-2026","title":"What is a parameter in 2026? The function input slot explained","excerpt":"A parameter is a named placeholder in a function definition that receives a value when the function is called. This guide explains parameters, how they differ from arguments, and the common types.","category":"code","date":"June 18, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":212}],"sameCategory":[{"slug":"bazel-build-explained-2026","title":"Bazel Explained: What Hermetic Builds Actually Cost","excerpt":"Bazel delivers reproducible, precisely cached builds and demands that you describe everything. Here is what you get, what it costs, and who should say no.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":145},{"slug":"graphql-federation-2026","title":"GraphQL Federation: One Graph, Many Teams","excerpt":"Federation composes independently-owned subgraphs into a single API. Here is what it solves, the operational cost it adds, and when a simpler gateway is enough.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":300},{"slug":"hermetic-builds-explained-2026","title":"Hermetic Builds Explained: Same Inputs, Same Output","excerpt":"A hermetic build depends only on declared inputs. Here is what that property gives you, the common leaks that break it, and how to move toward it incrementally.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":130},{"slug":"monorepo-tooling-compared-2026","title":"Monorepo Tooling Compared: What You Actually Need","excerpt":"Monorepo tools solve task orchestration and caching, not repository layout. Here is what each tier provides and how to tell when you have outgrown the simple option.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150}],"newer":{"slug":"parkinsons-law-2026","title":"Parkinsons Law in 2026: why work expands to fit time","excerpt":"Parkinsons Law says work expands to fill the time available for it. Here is the 2026 guide to what that really means, how to use tight deadlines on purpose, and where the idea gets oversold.","category":"guides","date":"June 30, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":320},"older":{"slug":"passkeys-vs-2fa-2026","title":"Passkeys vs 2FA Explained 2026: What Stops Phishing","excerpt":"Passkeys and two-factor authentication both aim to protect logins, but only one is phishing-resistant by design. Here is the 2026 guide to how passkeys work, how they compare to 2FA, and when to use each.","category":"tech","date":"June 30, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":310}}