Functions
Default Arguments
Default arguments fill in a parameter when the caller leaves an argument out.
Default Arguments
default_arguments.cpp
#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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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 = ;
int times = ;
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;
}
default argument
A default argument is written in the function declaration and used only when that argument is omitted.