bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Functions
C++•C++ Functions

C++ Default Parameters

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ Default Parameters?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ myFunction( string country = "Norway" ) {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

If we call the function without an argument, it uses the default value ("Norway"):
You can also use a default parameter value, by using the equals sign ( = ).
Default Parameter Value

Default Parameter Value

You can also use a default parameter value, by using the equals sign ( = ).

If we call the function without an argument, it uses the default value ("Norway"):

Example

void myFunction( string country = "Norway" ) {
  cout
  << country << "\n";
}
int main() {
  myFunction("Sweden");
  myFunction("India");
  myFunction();
  myFunction("USA");
  return 0;
}
// Sweden // India // Norway // USA

A parameter with a default value, is often known as an " optional parameter ". From the example above, country is an optional parameter and "Norway" is the default value.

Previous

C++ Function Parameters

Next

C++ Multiple Parameters