Small Steps V — Making Your Own Types

Use References

Use References

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Naming the original instead of copying

What you will learn

A reference is another name for an existing object. An int& parameter refers to the original integer passed in.

Try it

Example · example1.cpp
void add_one(int x)
{
    x = x + 1;
    print("Inside: ", x);
}

void small_main()
{
    int n = 10;

    add_one(n);

    print("Outside: ", n);
}

First run the example: it prints Inside: 11 and Outside: 10. Change add_one(int x) to add_one(int& x) and run again. Now both are 11.

Initially x was a copy; after adding &, it is another name for n. Use this when you intend to change the original.

Reading without copying

What you will learn

A const reference lets you read the original without copying it, while promising not to change it through that reference.

Try it

Example · references_2_example2.cpp
int sum(const Array<int>& numbers)
{
    int total = 0;

    for (int i = 0; i < numbers.length(); i = i + 1)
        total = total + numbers[i];

    return total;
}

void small_main()
{
    Array<int> numbers = {3, 7, 2, 9, 4};

    print("Sum: ", sum(numbers));
}

In const Array<int>& numbers, & marks a reference to the original and const makes it read-only. The output is Sum: 25.

The sum needs no copy of the whole array. Attempting to change it causes a compilation error. The earlier Array<int> parameter is also valid, but copying can be costly for large arrays.

Exercise

Create swap(int& a, int& b) to exchange the original values of two ints.

Exercise starter · exercise1_starter.cpp
void swap(int& a, int& b)
{
    // swap the original values.
}

void small_main()
{
    int x = 3;
    int y = 7;

    swap(x, y);

    print(x, ", ", y);
}

Hint

Save a temporarily in temp, then assign a=b and b=temp.

show solution
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void small_main()
{
    int x = 3;
    int y = 7;

    swap(x, y);

    print(x, ", ", y);
}
Try More (Optional)

Exercise

Change the earlier find_largest(Array<int> numbers) to use const Array<int>&. The function does not modify the Array.

Exercise starter · references_2_exercise2_starter.cpp
int find_largest(Array<int> numbers)
{
    int largest = numbers[0];

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] > largest)
            largest = numbers[i];

    return largest;
}

void small_main()
{
    Array<int> numbers = {4, 12, 3, 9};
    print(find_largest(numbers));
}

Hint

Change only the parameter to const Array<int>& numbers; the algorithm can stay the same.

show solution
int find_largest(const Array<int>& numbers)
{
    int largest = numbers[0];

    for (int i = 1; i < numbers.length(); i = i + 1)
        if (numbers[i] > largest)
            largest = numbers[i];

    return largest;
}

void small_main()
{
    Array<int> numbers = {4, 12, 3, 9};
    print(find_largest(numbers));
}
← Previous lessonNext lesson →