📖 Scenario: Imagine you have a simple program that needs to update a number by doubling it. Instead of returning the new value, you want the original number to change directly when you call a function.
🎯 Goal: Build a C++ program that uses a function to double a number by passing the number by reference. This means the function will change the original number directly.
📋 What You'll Learn
Create an integer variable named num with the value 10.
Create a function named doubleValue that takes an integer parameter by reference.
Inside doubleValue, multiply the parameter by 2.
Call doubleValue with num as the argument.
Print the value of num after the function call.
💡 Why This Matters
🌍 Real World
Passing parameters by reference is useful when you want functions to modify data directly, such as updating settings, counters, or large data structures without copying them.
💼 Career
Understanding parameter passing is important for writing efficient and clear C++ code, which is a key skill in software development, game programming, and systems programming.
Progress0 / 4 steps
1
Create the initial variable
Create an integer variable called num and set it to 10.
C++
Hint
Use int num = 10; to create the variable.
2
Create the function with reference parameter
Create a function named doubleValue that takes an int parameter by reference named value. Inside the function, multiply value by 2.
C++
Hint
Use int& value to pass the parameter by reference.
3
Call the function with the variable
Inside main(), call the function doubleValue with the variable num as the argument.
C++
Hint
Call the function by writing doubleValue(num); inside main().
4
Print the updated value
Add a std::cout statement inside main() to print the value of num after calling doubleValue. The output should be exactly 20.
C++
Hint
Use std::cout << num << std::endl; to print the 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
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 D
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
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 uses int &num, which is the correct way to pass by reference.
Final Answer:
int &num -> Option A
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
Step 1: Understand function behavior
The function addOne takes x by reference and increments it by 1.
Step 2: Trace the variable a
Initially, a is 5. After calling addOne(a), a becomes 6.
Final Answer:
6 -> Option C
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
Step 1: Check function parameter syntax
The function swap correctly uses references int &a and int &b.
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.
Final Answer:
No error, code works correctly -> Option B
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
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.
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.