0
0
C++programming~10 mins

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

Choose your learning style9 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.