Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create 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 value1 representing the first number
  • An integer value2 representing the second number
  • An integer value3 representing the third number

Your program should:

  1. Create a function named tripleValues that takes three integer parameters by reference
  2. Inside the function, multiply each parameter by 3
  3. In the main function, read the three input values and store them in variables
  4. Print the original values before calling the function
  5. Call the tripleValues function with the three variables
  6. 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow