Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Functions and collections · Lesson 7 of 21

    References: by value or by reference?

    Does a function get a copy or the original, and why const& is everywhere.

    9 minutes2 games

    When you pass a variable to a function, C++ passes a copy (by value). Changes inside the function hit the copy; the original stays the same. To let the function change the original, pass it by reference with &.

    C++
    #include <iostream>
    
    void addBonusCopy(int xp) { xp += 50; }
    void addBonusRef(int& xp) { xp += 50; }
    
    void swapValues(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
    int main() {
        int xp = 100;
        addBonusCopy(xp);
        std::cout << "after copy: " << xp << "\n";
        addBonusRef(xp);
        std::cout << "after ref:  " << xp << "\n";
    
        int x = 1, y = 2;
        swapValues(x, y);
        std::cout << x << " " << y << "\n";
        return 0;
    }
    xp (in main)100int
    xp (addBonusCopy)100 → 150a separate copy
    xp (addBonusRef)→ main's xpint& (another name)

    A reference isn't a new variable: it's another name for the same memory.

    Copying an int is cheap; copying a long std::string or a million-element vector isn't. So when a function only reads, pass a `const` reference: no copy, and no accidental changes.

    C++
    #include <iostream>
    #include <string>
    #include <vector>
    
    // Reads only: const& avoids copying the whole vector
    int total(const std::vector<int>& scores) {
        int sum = 0;
        for (int s : scores) sum += s;
        return sum;
    }
    
    int main() {
        std::vector<int> scores = {90, 75, 88};
        std::cout << total(scores) << "\n";
    
        int lives = 3;
        int& alias = lives;    // alias and lives are the same int
        alias--;
        std::cout << lives << "\n";
        return 0;
    }

    Important

    Rule of thumb: pass small types (int, double, char, bool) by value; pass big ones (string, vector, classes) by const& to read, or & when the function must change them. Never return a reference to a local variable: it dies when the function ends.

    What's the output?

    #include <iostream>
    void f(int a, int& b) {
        a = a * 10;
        b = b * 10;
    }
    int main() {
        int x = 2, y = 3;
        f(x, y);
        std::cout << x << " " << y << "\n";
    }

    What will this code print?

    Interview question

    What's the difference between a reference and a pointer?

    Exercise

    Write void doubleAll(std::vector<int>& v) that doubles every element in place. The program should print 2 4 6.

    Expected output: 2 4 6

    C++
    #include <iostream>
    #include <vector>
    
    void doubleAll(std::vector<int>& v) {
        for (int x : v) {
            x *= 2;
        }
    }
    
    int main() {
        std::vector<int> v = {1, 2, 3};
        doubleAll(v);
        for (int x : v) std::cout << x << " ";
        std::cout << "\n";
        return 0;
    }

    Learn more from

    • learncpp: Lvalue referenceslearncpp.com
    • learncpp: Pass by lvalue referencelearncpp.com
    • C++ Core Guidelines: Parameter passingisocpp.github.io

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousFunctionsNextStrings and arrays
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.