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"):
12345678910void myFunc(string name, string country = "Canada") { cout << name << " is from " << country << endl; } int main() { // Call the function to be executed myFunc("Ted", "USA"); myFunc("Robin"); return 0; }
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
.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Posez-moi des questions sur ce sujet
Résumer ce chapitre
Afficher des exemples du monde réel
Awesome!
Completion rate improved to 2.94
Default
Glissez pour afficher le menu
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"):
12345678910void myFunc(string name, string country = "Canada") { cout << name << " is from " << country << endl; } int main() { // Call the function to be executed myFunc("Ted", "USA"); myFunc("Robin"); return 0; }
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
.
Merci pour vos commentaires !