🚀 Alguni Start learning

Unit 3: Functions & References

Two names for one thing.

Unit 3 of 8 in C++ for kids. Its 4 lessons are Making Functions, Copies and Originals, Handy Extras and Function Factory — below is everything each one explains, and a question or two from it to try.

Every sample on this page was compiled and run with g++ before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🧰 Making Functions

Exactly like C

Say what comes back, then the name, then what goes in. Nothing runs until you *call* it.

C++

int double_it(int n) {
    return n * 2;
}

int main() {
    cout << double_it(5) << endl;
    return 0;
}

It prints

10

Functions that hand nothing back

A function that only *does* something has the type void.

C++

void cheer(string who) {
    cout << "Go " + who + "!" << endl;
}

int main() {
    cheer("Ada");
    return 0;
}

It prints

Go Ada!

Try it yourself

What does this print?

C++

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << add(2, 3) + add(1, 1) << endl;
    return 0;
}

Why must a function be written ABOVE main?

  • It looks tidier
  • C++ reads top to bottom, so it has to know the function before main uses it
  • main must be last in the file
  • It does not matter

Answer them in the app

📄 Copies and Originals

A function gets a copy

Hand a value to a function and it works on its own copy. Change it inside and the original never notices — the same trap you met in C.

C++

void tryToChange(int n) {
    n = 99;
}

int main() {
    int score = 5;
    tryToChange(score);
    cout << score << endl;
    return 0;
}

It prints

5

A reference is another name

Put & after the type and the parameter stops being a copy — it becomes another name for the very same variable. C needed a pointer and a * for this; C++ just uses the name.

C++

void grow(int& n) {
    n = n + 10;
}

int main() {
    int score = 5;
    grow(score);
    cout << score << endl;
    return 0;
}

It prints

15

Try it yourself

What does this print?

C++

void reset(int& n) {
    n = 0;
}

int main() {
    int lives = 3;
    reset(lives);
    cout << lives << endl;
    return 0;
}

And this one? Look closely at the & — it is missing.

C++

void reset(int n) {
    n = 0;
}

int main() {
    int lives = 3;
    reset(lives);
    cout << lives << endl;
    return 0;
}

Answer them in the app

🎁 Handy Extras

Values you can leave out

Give a parameter a default and callers may skip it. Handy when there is an obvious usual answer.

C++

int power(int base, int times = 2) {
    int total = 1;
    for (int i = 0; i < times; i++) {
        total = total * base;
    }
    return total;
}

int main() {
    cout << power(3) << endl;
    cout << power(3, 3) << endl;
    return 0;
}

It prints

9
27

Passing big things cheaply

A vector handed in by value gets copied — slow for a big one. A reference avoids the copy, and lets the function change it.

C++

void addOne(vector<int>& v) {
    v.push_back(1);
}

int main() {
    vector<int> nums;
    addOne(nums);
    addOne(nums);
    cout << nums.size() << endl;
    return 0;
}

It prints

2

Try it yourself

What does this print?

C++

void shout(string word, string mark = "!") {
    cout << word + mark << endl;
}

int main() {
    shout("go");
    shout("stop", "?");
    return 0;
}

What does this print? (No & this time.)

C++

void addOne(vector<int> v) {
    v.push_back(1);
}

int main() {
    vector<int> nums;
    addOne(nums);
    cout << nums.size() << endl;
    return 0;
}

Answer them in the app

🏆 Function Factory

Try it yourself

What does this print?

C++

void swapThem(int& a, int& b) {
    int keep = a;
    a = b;
    b = keep;
}

int main() {
    int x = 1;
    int y = 2;
    swapThem(x, y);
    cout << x << y << endl;
    return 0;
}

What does this print?

C++

int countdown(int from) {
    if (from <= 0) {
        return 0;
    }
    cout << from;
    return countdown(from - 1);
}

int main() {
    countdown(4);
    cout << endl;
    return 0;
}

Answer them in the app