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!
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 2.94
Parameters
Pyyhkäise näyttääksesi valikon
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!
Kiitos palautteestasi!