Pass by Reference
Part of the Logic & Flow section of Coddy's C++ journey — lesson 41 of 56.
When you pass arguments to functions, C++ normally creates copies of the values. This means changes inside the function don't affect the original variables. Pass by reference changes this behavior by allowing functions to work directly with the original variables.
To pass by reference, add the & symbol after the parameter type in your function declaration:
void doubleValue(int& number) {
number = number * 2; // Modifies the original variable
}When you call this function, any changes made to the parameter inside the function will directly modify the original variable that was passed in. This is particularly useful when you want a function to modify its arguments or when working with large objects where copying would be inefficient.
Pass by reference eliminates the overhead of copying data, making your programs faster and more memory-efficient, especially with complex data structures like vectors or maps.
Challenge
EasyCreate a function that demonstrates pass by reference by modifying variables directly through function parameters. This challenge will test your understanding of how the & symbol allows functions to work with original variables instead of copies.
The following inputs will be provided:
- An integer
value1representing the first number - An integer
value2representing the second number - An integer
value3representing the third number
Your program should:
- Create a function named
tripleValuesthat takes three integer parameters by reference - Inside the function, multiply each parameter by 3
- In the main function, read the three input values and store them in variables
- Print the original values before calling the function
- Call the
tripleValuesfunction with the three variables - Print the modified values after the function call to show they were changed
Use the following exact output format:
Before function call:
Original values: [value1] [value2] [value3]After function call:
Tripled values: [new_value1] [new_value2] [new_value3]The function signature should use the & symbol after each parameter type to indicate pass by reference: void tripleValues(int& a, int& b, int& c). This allows the function to directly modify the original variables passed to it, demonstrating the key difference between pass by value and pass by reference. The variables in main will be permanently changed after the function call, proving that the function worked with the original memory locations rather than copies.
Cheat sheet
To pass by reference in C++, add the & symbol after the parameter type in your function declaration:
void doubleValue(int& number) {
number = number * 2; // Modifies the original variable
}Pass by reference allows functions to work directly with the original variables instead of copies. Changes made to parameters inside the function will modify the original variables that were passed in.
This eliminates the overhead of copying data, making programs faster and more memory-efficient, especially with complex data structures like vectors or maps.
Try it yourself
#include <iostream>
using namespace std;
// TODO: Create the tripleValues function here
int main() {
// Read input values
int value1, value2, value3;
cin >> value1 >> value2 >> value3;
// Print original values
cout << "Original values: " << value1 << " " << value2 << " " << value3 << endl;
// TODO: Call the tripleValues function here
// Print modified values
cout << "Tripled values: " << value1 << " " << value2 << " " << value3 << endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice4Maps (Key-Value Pairs)
Introducing std::mapCreating a MapAccessing and Modifying ValuesChecking for KeysRemoving PairsIterating Over a MapRecap - Word Frequency7Advanced Functions
Pass by ReferenceIntro Lambda ExpressionsLambdas with ParametersLambdas with Return ValuesIntroduction to RecursionRecursive FactorialLambda Sort2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items