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!
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 2.94
Parameters
Swipe to show 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!
Thanks for your feedback!