Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Default | Functions
C++ Intermediate | Mobile-Friendly
course content

Kursusindhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Default

During the function definition, you can set default values for the last parameters. For example, you want to print the country the user is from, but if you don't have this information, there is a way to print the default value ("Canada"):

12345678910
void myFunc(string name, string country = "Canada") { &nbsp;&nbsp;&nbsp;&nbsp;cout << name << " is from " << country << endl; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;// Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp;myFunc("Ted", "USA"); &nbsp;&nbsp;&nbsp;&nbsp;myFunc("Robin"); &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here by the second function call, we don’t know the country Robin is from, so the function myFunc() used the default value for the country - Canada.

question-icon

Set the defualt value of age equal to 20:

myFunc(string name, int)

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Default

During the function definition, you can set default values for the last parameters. For example, you want to print the country the user is from, but if you don't have this information, there is a way to print the default value ("Canada"):

12345678910
void myFunc(string name, string country = "Canada") { &nbsp;&nbsp;&nbsp;&nbsp;cout << name << " is from " << country << endl; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;// Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp;myFunc("Ted", "USA"); &nbsp;&nbsp;&nbsp;&nbsp;myFunc("Robin"); &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here by the second function call, we don’t know the country Robin is from, so the function myFunc() used the default value for the country - Canada.

question-icon

Set the defualt value of age equal to 20:

myFunc(string name, int)

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
Vi beklager, at noget gik galt. Hvad skete der?
some-alt