Parameters
Let’s imagine we have 3 pets: dog, cat, and pig. They all make different sounds, and it would be silly to write 3 different functions with the same functionality (outputting words). Here we can use parameters! Parameters (or arguments) act like variables inside the function and are specified with their type inside the parentheses after the function. You can use as many arguments as you want:
Code:
12345678910111213// Function declaration void myFunc(string animal, string sound) { cout << animal << " says " << sound << endl; } int main() { // Call the function to be executed myFunc("cat", "meow"); myFunc("dog", "bow"); myFunc("pig", "wee"); return 0; }
Variables you use can be any type, and your function can also accept different types simultaneously!
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
Parameters
Glissez pour afficher le menu
Let’s imagine we have 3 pets: dog, cat, and pig. They all make different sounds, and it would be silly to write 3 different functions with the same functionality (outputting words). Here we can use parameters! Parameters (or arguments) act like variables inside the function and are specified with their type inside the parentheses after the function. You can use as many arguments as you want:
Code:
12345678910111213// Function declaration void myFunc(string animal, string sound) { cout << animal << " says " << sound << endl; } int main() { // Call the function to be executed myFunc("cat", "meow"); myFunc("dog", "bow"); myFunc("pig", "wee"); return 0; }
Variables you use can be any type, and your function can also accept different types simultaneously!
Merci pour vos commentaires !