A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Parameter Passing: Value vs. Reference

College Depth 94 in the knowledge graph ☐ I know this ☆ Set as goal
504prerequisites beneath it
See this on the map →
Function Parameters and Argument PassingScope, Shadowing, and Variable Lifetime→
functions parameters memory

Core Idea

Pass-by-value creates a copy; changes inside the function don't affect the original. Pass-by-reference passes the actual variable; changes are visible outside. Some languages default to one, others allow choosing (ref, &). Understanding which applies prevents bugs.

How It's Best Learned

Modify parameters inside functions and check if the original changed; test with different types (primitives vs objects); use language-specific tools to track memory.

Common Misconceptions

That all languages use the same passing strategy (they don't); that pass-by-reference is always better (pass-by-value is safer); that objects and primitives follow the same rules (often they don't).

Explainer

From your work with function parameters, you know that functions receive data through their parameter list. But what exactly happens when you pass a variable to a function? Does the function get its own copy, or does it get access to the original? The answer determines whether changes made inside the function are visible to the caller, and getting this wrong is one of the most common sources of subtle bugs.

Pass-by-value means the function receives a *copy* of the argument's value. The parameter inside the function is a completely independent variable that happens to start with the same value. If the function modifies its parameter, the original variable in the caller is unaffected. Think of it like photocopying a document and handing someone the copy — they can scribble all over it, and your original is untouched. In C, all parameters are passed by value by default. In Python, integers and strings behave this way because they are immutable — the function cannot modify the original object even though it has a reference to it.

Pass-by-reference means the function receives a direct handle to the original variable, not a copy. Any changes the function makes to the parameter are immediately visible to the caller. In C++, you opt into this with the `&` syntax: `void increment(int &x) { x++; }` will actually modify the caller's variable. This is powerful — it lets functions return multiple results by modifying their parameters, and it avoids the cost of copying large data structures. But it is also dangerous, because any function call might silently change your variables. When debugging, pass-by-reference means you cannot assume a variable's value is unchanged after a function call without checking.

Many languages use a third strategy that confuses beginners: pass-by-object-reference (sometimes called pass-by-sharing). In Python and Java, when you pass a list or object, the function receives a copy of the *reference* (pointer) to the object, not a copy of the object itself. This means the function can modify the object's contents (append to a list, change a field), and the caller sees those changes. But if the function reassigns the parameter to a completely new object, the caller's variable still points to the original. This is why `def f(lst): lst.append(1)` modifies the caller's list, but `def f(lst): lst = [1, 2, 3]` does not. Understanding this distinction — "the reference is copied, but the object is shared" — resolves most of the confusion about parameter passing in modern languages.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Scope and Binding Resolution → Scope, Shadowing, and Variable Lifetime → Parameter Passing: Value vs. Reference

Longest path: 95 steps · 504 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.