Bird
Raised Fist0
C++programming~10 mins

Passing parameters by reference in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Passing parameters by reference
Function call with variable
↓
Parameter receives reference
↓
Function modifies parameter
↓
Original variable changed
↓
Function returns
↓
Program continues with updated variable
When a function parameter is passed by reference, the function works directly with the original variable, so changes inside the function affect the original.
Execution Sample
C++
void addOne(int &num) {
    num = num + 1;
}

int main() {
    int a = 5;
    addOne(a);
}
This code increases the value of 'a' by 1 using a function that takes 'a' by reference.
Execution Table
StepVariableValue BeforeActionValue After
1a5Call addOne(a), num references a5
2num5Add 1 to num (num = num + 1)6
3a5num is reference to a, so a changes too6
4FunctionRunningReturn from addOneEnd
5a6Program continues with updated a6
💡 Function ends, original variable 'a' is updated to 6 because 'num' was a reference.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
a5566
num5666
Key Moments - 2 Insights
Why does changing 'num' inside the function change 'a' outside?
Because 'num' is a reference to 'a' (see Step 1 and Step 3 in execution_table), both names point to the same memory, so changing 'num' changes 'a'.
What if we passed 'num' by value instead of reference?
If passed by value, 'num' would be a copy of 'a', so changes to 'num' inside the function would not affect 'a' (not shown in this table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'a' after Step 3?
A6
B5
C7
DUndefined
💡 Hint
Check the 'Value After' column for 'a' at Step 3 in execution_table.
At which step does the function add 1 to the parameter?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column describing 'Add 1 to num' in execution_table.
If 'num' was passed by value, how would 'a' change after the function call?
A'a' would become 6
B'a' would remain 5
C'a' would become 0
D'a' would be undefined
💡 Hint
Refer to key_moments explanation about passing by value vs reference.
Concept Snapshot
Passing parameters by reference in C++ uses & in the function parameter.
The function works with the original variable, not a copy.
Changes inside the function affect the original variable.
Syntax example: void func(int &x) { x = x + 1; }
Call with variable: func(a);
Useful to modify variables directly.
Full Transcript
This visual trace shows how passing parameters by reference works in C++. When the function addOne is called with variable 'a', the parameter 'num' becomes a reference to 'a'. Inside the function, adding 1 to 'num' changes 'a' directly because they share the same memory. The execution table tracks each step, showing 'a' changing from 5 to 6. Key moments clarify why changes affect the original variable and what would happen if passed by value instead. The quiz tests understanding of variable values at different steps and the difference between passing by reference and by value.

Practice

(1/5)
1.

What does passing a parameter by reference in C++ allow a function to do?

easy
A. Only read the variable without access to its value
B. Create a copy of the variable inside the function
C. Prevent the variable from being changed
D. Modify the original variable passed to the function

Solution

  1. Step 1: Understand passing by reference

    Passing by reference means the function receives the actual variable's address, not a copy.
  2. Step 2: Effect on the original variable

    Since the function works with the original variable, any changes inside the function affect it directly.
  3. Final Answer:

    Modify the original variable passed to the function -> Option D
  4. Quick Check:

    Passing by reference = modify original [OK]
Hint: Reference means function can change original variable [OK]
Common Mistakes:
  • Thinking it creates a copy instead of using original
  • Confusing with passing by value
  • Assuming it prevents changes
2.

Which of the following is the correct syntax to pass an integer num by reference to a function update?

void update(??? num) { num = 10; }
easy
A. int &num
B. int *num
C. int num
D. int &&num

Solution

  1. Step 1: Identify reference syntax in C++

    To pass by reference, use & after the type in the parameter list.
  2. Step 2: Match syntax to options

    int &num uses int &num, which is the correct way to pass by reference.
  3. Final Answer:

    int &num -> Option A
  4. Quick Check:

    Reference parameter uses & after type [OK]
Hint: Use & after type to pass by reference [OK]
Common Mistakes:
  • Using pointer syntax (*) instead of reference (&)
  • Omitting & and passing by value
  • Confusing rvalue reference (&&) with normal reference
3.

What is the output of the following code?

void addOne(int &x) {
    x = x + 1;
}

int main() {
    int a = 5;
    addOne(a);
    std::cout << a << std::endl;
    return 0;
}
medium
A. Compilation error
B. 5
C. 6
D. Undefined behavior

Solution

  1. Step 1: Understand function behavior

    The function addOne takes x by reference and increments it by 1.
  2. Step 2: Trace the variable a

    Initially, a is 5. After calling addOne(a), a becomes 6.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    Reference parameter increments original variable [OK]
Hint: Reference changes original variable value [OK]
Common Mistakes:
  • Thinking original variable stays unchanged
  • Confusing reference with pointer syntax
  • Expecting compilation error due to missing &
4.

Identify the error in this code snippet:

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    swap(x, y);
    std::cout << x << ", " << y << std::endl;
    return 0;
}
medium
A. Missing & in function parameters
B. No error, code works correctly
C. Incorrect swapping logic
D. Missing return type in main

Solution

  1. Step 1: Check function parameter syntax

    The function swap correctly uses references int &a and int &b.
  2. Step 2: Verify swapping logic and main function

    The swapping logic uses a temporary variable correctly. The main function has proper return type and prints swapped values.
  3. Final Answer:

    No error, code works correctly -> Option B
  4. Quick Check:

    Correct reference swap function = no error [OK]
Hint: Check reference syntax and logic carefully [OK]
Common Mistakes:
  • Assuming swap logic is wrong
  • Confusing pointers with references
  • Thinking main lacks return type
5.

Given this function that doubles the value of an integer passed by reference, what will be the output of the program?

void doubleValue(int &num) {
    num *= 2;
}

int main() {
    int values[] = {1, 2, 3};
    for (int i = 0; i < 3; i++) {
        doubleValue(values[i]);
    }
    for (int v : values) {
        std::cout << v << " ";
    }
    return 0;
}
hard
A. 2 4 6
B. 1 2 3
C. Compilation error due to reference in array
D. 0 0 0

Solution

  1. Step 1: Understand passing array elements by reference

    Each element values[i] is passed by reference to doubleValue, so the original array elements are modified.
  2. Step 2: Trace the loop and doubling effect

    Loop runs 3 times, doubling each element: 1->2, 2->4, 3->6.
  3. Final Answer:

    2 4 6 -> Option A
  4. Quick Check:

    Reference parameter doubles original array elements [OK]
Hint: Array elements passed by reference get changed directly [OK]
Common Mistakes:
  • Thinking array elements are copied, not changed
  • Expecting compilation error with references and arrays
  • Ignoring loop effect on all elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard