Default arguments fill in a parameter when the caller leaves an argument out.

default argument A default argument is written in the function declaration and used only when that argument is omitted.

Default Arguments

example
default_arguments.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>

std::string repeatWord(std::string word, int times = 1) {
    std::string result = "";
    for (int i = 0; i < times; i++) {
        result += word;
    }
    return result;
}

int main() {
    std::string word = "go";
    int times = 2;

    std::string once = repeatWord(word);
    std::string repeated = repeatWord(word, times);

    std::cout << "once=" << once << std::endl;
    std::cout << "repeated=" << repeated << std::endl;
    return 0;
}
#include <iostream>
#include <string>

std::string repeatWord(std::string word, int times = 1) {
    std::string result = "";
    for (int i = 0; i < times; i++) {
        result += word;
    }
    return result;
}

int main() {
    std::string word = "run";
    int times = 2;

    std::string once = repeatWord(word);
    std::string repeated = repeatWord(word, times);

    std::cout << "once=" << once << std::endl;
    std::cout << "repeated=" << repeated << std::endl;
    return 0;
}
#include <iostream>
#include <string>

std::string repeatWord(std::string word, int times = 1) {
    std::string result = "";
    for (int i = 0; i < times; i++) {
        result += word;
    }
    return result;
}

int main() {
    std::string word = "ship";
    int times = 2;

    std::string once = repeatWord(word);
    std::string repeated = repeatWord(word, times);

    std::cout << "once=" << once << std::endl;
    std::cout << "repeated=" << repeated << std::endl;
    return 0;
}
#include <iostream>
#include <string>

std::string repeatWord(std::string word, int times = 1) {
    std::string result = "";
    for (int i = 0; i < times; i++) {
        result += word;
    }
    return result;
}

int main() {
    std::string word = "go";
    int times = 3;

    std::string once = repeatWord(word);
    std::string repeated = repeatWord(word, times);

    std::cout << "once=" << once << std::endl;
    std::cout << "repeated=" << repeated << std::endl;
    return 0;
}
#include <iostream>
#include <string>

std::string repeatWord(std::string word, int times = 1) {
    std::string result = "";
    for (int i = 0; i < times; i++) {
        result += word;
    }
    return result;
}

int main() {
    std::string word = "go";
    int times = 4;

    std::string once = repeatWord(word);
    std::string repeated = repeatWord(word, times);

    std::cout << "once=" << once << std::endl;
    std::cout << "repeated=" << repeated << std::endl;
    return 0;
}
  1. word ← go, times ← 2

    12int main() {13    std::string word→ go = "go"; //@word="run", "ship"14    int times→ 2 = 2; //@times=3, 41516    std::string once = repeatWord(wordgo);17    std::string repeated = repeatWord(word, times);
  2. result ← (empty)

    pass 1 of 2
    4std::string repeatWord(std::string wordgo, int times1 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  3. result ← go

    pass 1 of 3
    5std::string result = "";6for (int i0 = 0; i < times1; i++) {7    result→ go += wordgo;8}
    All 3 passes — pass 1 is the card above
    passitimesresult
    101(empty) go
    202(empty) go
    312go gogo
  4. return result;

    8    }9    return resultgo;10}
  5. once ← go

    16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times2);
  6. result ← (empty)

    pass 2 of 2
    4std::string repeatWord(std::string wordgo, int times2 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  7. return result;

    8    }9    return resultgogo;10}
  8. repeated ← gogo

    16    std::string once = repeatWord(word);17    std::string repeated→ gogo = repeatWord(wordgo, times2);1819    std::cout << "once=" << oncego << std::endl;20    std::cout << "repeated=" << repeatedgogo << std::endl;21    return 0;22}
    outputonce=go
    repeated=gogo
  1. word ← run, times ← 2

    12int main() {13    std::string word→ run = "run";14    int times→ 2 = 2;1516    std::string once = repeatWord(wordrun);17    std::string repeated = repeatWord(word, times);
  2. result ← (empty)

    pass 1 of 2
    4std::string repeatWord(std::string wordrun, int times1 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  3. result ← run

    pass 1 of 3
    5std::string result = "";6for (int i0 = 0; i < times1; i++) {7    result→ run += wordrun;8}
    All 3 passes — pass 1 is the card above
    passitimesresult
    101(empty) run
    202(empty) run
    312run runrun
  4. return result;

    8    }9    return resultrun;10}
  5. once ← run

    16std::string once→ run = repeatWord(wordrun);17std::string repeated = repeatWord(wordrun, times2);
  6. result ← (empty)

    pass 2 of 2
    4std::string repeatWord(std::string wordrun, int times2 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  7. return result;

    8    }9    return resultrunrun;10}
  8. repeated ← runrun

    16    std::string once = repeatWord(word);17    std::string repeated→ runrun = repeatWord(wordrun, times2);1819    std::cout << "once=" << oncerun << std::endl;20    std::cout << "repeated=" << repeatedrunrun << std::endl;21    return 0;22}
    outputonce=run
    repeated=runrun
  1. word ← ship, times ← 2

    12int main() {13    std::string word→ ship = "ship";14    int times→ 2 = 2;1516    std::string once = repeatWord(wordship);17    std::string repeated = repeatWord(word, times);
  2. result ← (empty)

    pass 1 of 2
    4std::string repeatWord(std::string wordship, int times1 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  3. result ← ship

    pass 1 of 3
    5std::string result = "";6for (int i0 = 0; i < times1; i++) {7    result→ ship += wordship;8}
    All 3 passes — pass 1 is the card above
    passitimesresult
    101(empty) ship
    202(empty) ship
    312ship shipship
  4. return result;

    8    }9    return resultship;10}
  5. once ← ship

    16std::string once→ ship = repeatWord(wordship);17std::string repeated = repeatWord(wordship, times2);
  6. result ← (empty)

    pass 2 of 2
    4std::string repeatWord(std::string wordship, int times2 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  7. return result;

    8    }9    return resultshipship;10}
  8. repeated ← shipship

    16    std::string once = repeatWord(word);17    std::string repeated→ shipship = repeatWord(wordship, times2);1819    std::cout << "once=" << onceship << std::endl;20    std::cout << "repeated=" << repeatedshipship << std::endl;21    return 0;22}
    outputonce=ship
    repeated=shipship
  1. word ← go, times ← 3

    12int main() {13    std::string word→ go = "go";14    int times→ 3 = 3;1516    std::string once = repeatWord(wordgo);17    std::string repeated = repeatWord(word, times);
  2. result ← (empty)

    pass 1 of 2
    4std::string repeatWord(std::string wordgo, int times1 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  3. result ← go

    pass 1 of 4
    5std::string result = "";6for (int i0 = 0; i < times1; i++) {7    result→ go += wordgo;8}
    All 4 passes — pass 1 is the card above
    passitimesresult
    101(empty) go
    203(empty) go
    313go gogo
    423gogo gogogo
  4. return result;

    8    }9    return resultgo;10}
  5. once ← go

    16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times3);
  6. result ← (empty)

    pass 2 of 2
    4std::string repeatWord(std::string wordgo, int times3 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  7. return result;

    8    }9    return resultgogogo;10}
  8. repeated ← gogogo

    16    std::string once = repeatWord(word);17    std::string repeated→ gogogo = repeatWord(wordgo, times3);1819    std::cout << "once=" << oncego << std::endl;20    std::cout << "repeated=" << repeatedgogogo << std::endl;21    return 0;22}
    outputonce=go
    repeated=gogogo
  1. word ← go, times ← 4

    12int main() {13    std::string word→ go = "go";14    int times→ 4 = 4;1516    std::string once = repeatWord(wordgo);17    std::string repeated = repeatWord(word, times);
  2. result ← (empty)

    pass 1 of 2
    4std::string repeatWord(std::string wordgo, int times1 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  3. result ← go

    pass 1 of 5
    5std::string result = "";6for (int i0 = 0; i < times1; i++) {7    result→ go += wordgo;8}
    All 5 passes — pass 1 is the card above
    passitimesresult
    101(empty) go
    204(empty) go
    314go gogo
    424gogo gogogo
    534gogogo gogogogo
  4. return result;

    8    }9    return resultgo;10}
  5. once ← go

    16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times4);
  6. result ← (empty)

    pass 2 of 2
    4std::string repeatWord(std::string wordgo, int times4 = 1) {5    std::string result→ (empty) = "";6    for (int i = 0; i < times; i++) {
  7. return result;

    8    }9    return resultgogogogo;10}
  8. repeated ← gogogogo

    16    std::string once = repeatWord(word);17    std::string repeated→ gogogogo = repeatWord(wordgo, times4);1819    std::cout << "once=" << oncego << std::endl;20    std::cout << "repeated=" << repeatedgogogogo << std::endl;21    return 0;22}
    outputonce=go
    repeated=gogogogo

Follow the Calls

  1. word starts as go.
  2. times starts as 2.
  3. repeatWord(word) omits times, so the default 1 is used.
  4. once becomes go.
  5. repeatWord(word, times) repeats go twice, so repeated becomes gogo. | call | times used | result | | --- | --- | --- | | repeatWord("go") | 1 | go | | repeatWord("go", 2) | 2 | gogo |

Exercise: default_arguments.cpp

Reproduce once=go and repeated=gogo, then change only word to run to predict once=run repeated=runrun, and change only times to 3 to predict repeated=gogogo.