Functions
Default Arguments
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
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;
}
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);result ← (empty)
pass 1 of 24std::string repeatWord(std::string wordgo, int times1 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {result ← go
pass 1 of 35std::string result = "";6for (int i0 = 0; i < times1; i++) {7 result→ go += wordgo;8}All 3 passes — pass 1 is the card above pass itimesresult1 0 1 (empty) → go 2 0 2 (empty) → go 3 1 2 go → gogo return result;
8 }9 return resultgo;10}once ← go
16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times2);result ← (empty)
pass 2 of 24std::string repeatWord(std::string wordgo, int times2 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {return result;
8 }9 return resultgogo;10}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
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);result ← (empty)
pass 1 of 24std::string repeatWord(std::string wordrun, int times1 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {result ← run
pass 1 of 35std::string result = "";6for (int i0 = 0; i < times1; i++) {7 result→ run += wordrun;8}All 3 passes — pass 1 is the card above pass itimesresult1 0 1 (empty) → run 2 0 2 (empty) → run 3 1 2 run → runrun return result;
8 }9 return resultrun;10}once ← run
16std::string once→ run = repeatWord(wordrun);17std::string repeated = repeatWord(wordrun, times2);result ← (empty)
pass 2 of 24std::string repeatWord(std::string wordrun, int times2 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {return result;
8 }9 return resultrunrun;10}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
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);result ← (empty)
pass 1 of 24std::string repeatWord(std::string wordship, int times1 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {result ← ship
pass 1 of 35std::string result = "";6for (int i0 = 0; i < times1; i++) {7 result→ ship += wordship;8}All 3 passes — pass 1 is the card above pass itimesresult1 0 1 (empty) → ship 2 0 2 (empty) → ship 3 1 2 ship → shipship return result;
8 }9 return resultship;10}once ← ship
16std::string once→ ship = repeatWord(wordship);17std::string repeated = repeatWord(wordship, times2);result ← (empty)
pass 2 of 24std::string repeatWord(std::string wordship, int times2 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {return result;
8 }9 return resultshipship;10}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
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);result ← (empty)
pass 1 of 24std::string repeatWord(std::string wordgo, int times1 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {result ← go
pass 1 of 45std::string result = "";6for (int i0 = 0; i < times1; i++) {7 result→ go += wordgo;8}All 4 passes — pass 1 is the card above pass itimesresult1 0 1 (empty) → go 2 0 3 (empty) → go 3 1 3 go → gogo 4 2 3 gogo → gogogo return result;
8 }9 return resultgo;10}once ← go
16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times3);result ← (empty)
pass 2 of 24std::string repeatWord(std::string wordgo, int times3 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {return result;
8 }9 return resultgogogo;10}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
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);result ← (empty)
pass 1 of 24std::string repeatWord(std::string wordgo, int times1 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {result ← go
pass 1 of 55std::string result = "";6for (int i0 = 0; i < times1; i++) {7 result→ go += wordgo;8}All 5 passes — pass 1 is the card above pass itimesresult1 0 1 (empty) → go 2 0 4 (empty) → go 3 1 4 go → gogo 4 2 4 gogo → gogogo 5 3 4 gogogo → gogogogo return result;
8 }9 return resultgo;10}once ← go
16std::string once→ go = repeatWord(wordgo);17std::string repeated = repeatWord(wordgo, times4);result ← (empty)
pass 2 of 24std::string repeatWord(std::string wordgo, int times4 = 1) {5 std::string result→ (empty) = "";6 for (int i = 0; i < times; i++) {return result;
8 }9 return resultgogogogo;10}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
wordstarts asgo.timesstarts as2.repeatWord(word)omitstimes, so the default1is used.oncebecomesgo.repeatWord(word, times)repeatsgotwice, sorepeatedbecomesgogo. | 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.