Passing parameters by reference lets a function change the original value of a variable. This saves memory and can make programs faster.
Passing parameters by reference in C++
void functionName(type ¶meterName) { // code that uses parameterName }
The ampersand (&) after the type means the parameter is passed by reference.
Changes to the parameter inside the function affect the original variable.
void increase(int &num) { num = num + 1; }
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
This program shows how the function addFive changes the original variable value by adding 5 to it.
#include <iostream> using namespace std; void addFive(int &number) { number += 5; } int main() { int value = 10; cout << "Before: " << value << endl; addFive(value); cout << "After: " << value << endl; return 0; }
Passing by reference allows the function to modify the original variable directly.
Be careful: changes inside the function affect the original data.
If you don't want the function to change the variable, use const reference.
Passing parameters by reference lets functions change original variables.
Use & after the type in the function parameter to pass by reference.
This method saves memory and can improve performance.
Practice
What does passing a parameter by reference in C++ allow a function to do?
Solution
Step 1: Understand passing by reference
Passing by reference means the function receives the actual variable's address, not a copy.Step 2: Effect on the original variable
Since the function works with the original variable, any changes inside the function affect it directly.Final Answer:
Modify the original variable passed to the function -> Option DQuick Check:
Passing by reference = modify original [OK]
- Thinking it creates a copy instead of using original
- Confusing with passing by value
- Assuming it prevents changes
Which of the following is the correct syntax to pass an integer num by reference to a function update?
void update(??? num) { num = 10; }Solution
Step 1: Identify reference syntax in C++
To pass by reference, use & after the type in the parameter list.Step 2: Match syntax to options
int &num usesint &num, which is the correct way to pass by reference.Final Answer:
int &num -> Option AQuick Check:
Reference parameter uses & after type [OK]
- Using pointer syntax (*) instead of reference (&)
- Omitting & and passing by value
- Confusing rvalue reference (&&) with normal reference
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;
}Solution
Step 1: Understand function behavior
The functionaddOnetakesxby reference and increments it by 1.Step 2: Trace the variable
Initially,aais 5. After callingaddOne(a),abecomes 6.Final Answer:
6 -> Option CQuick Check:
Reference parameter increments original variable [OK]
- Thinking original variable stays unchanged
- Confusing reference with pointer syntax
- Expecting compilation error due to missing &
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;
}Solution
Step 1: Check function parameter syntax
The functionswapcorrectly uses referencesint &aandint &b.Step 2: Verify swapping logic and main function
The swapping logic uses a temporary variable correctly. Themainfunction has proper return type and prints swapped values.Final Answer:
No error, code works correctly -> Option BQuick Check:
Correct reference swap function = no error [OK]
- Assuming swap logic is wrong
- Confusing pointers with references
- Thinking main lacks return type
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;
}Solution
Step 1: Understand passing array elements by reference
Each elementvalues[i]is passed by reference todoubleValue, so the original array elements are modified.Step 2: Trace the loop and doubling effect
Loop runs 3 times, doubling each element: 1->2, 2->4, 3->6.Final Answer:
2 4 6 -> Option AQuick Check:
Reference parameter doubles original array elements [OK]
- Thinking array elements are copied, not changed
- Expecting compilation error with references and arrays
- Ignoring loop effect on all elements
Ready to prove it?
Pass the test and this pattern moves unknown
