Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Parameters | Functions
C++ Intermediate | Mobile-Friendly

bookParameters

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) { &nbsp; cout << animal << " says " << sound << endl; } int main() { &nbsp;&nbsp;&nbsp;&nbsp; // Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp; myFunc("cat", "meow"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myFunc("dog", "bow"); &nbsp;&nbsp;&nbsp;&nbsp; myFunc("pig", "wee"); &nbsp;&nbsp;&nbsp;&nbsp; return 0; }
copy

Variables you use can be any type, and your function can also accept different types simultaneously!

question-icon

Create the function which output the name and the age of the person:

#include <iostream>
using namespace std;

myFunc(name,age){
     cout << name << " is " << age << " years old." << endl;
}

int main(){
    myFunc(
,2);
    myFunc("Fiona",
);
}
Liam is 2 years old.
Fiona is 20 years old.

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

Awesome!

Completion rate improved to 2.94

bookParameters

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) { &nbsp; cout << animal << " says " << sound << endl; } int main() { &nbsp;&nbsp;&nbsp;&nbsp; // Call the function to be executed &nbsp;&nbsp;&nbsp;&nbsp; myFunc("cat", "meow"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myFunc("dog", "bow"); &nbsp;&nbsp;&nbsp;&nbsp; myFunc("pig", "wee"); &nbsp;&nbsp;&nbsp;&nbsp; return 0; }
copy

Variables you use can be any type, and your function can also accept different types simultaneously!

question-icon

Create the function which output the name and the age of the person:

#include <iostream>
using namespace std;

myFunc(name,age){
     cout << name << " is " << age << " years old." << endl;
}

int main(){
    myFunc(
,2);
    myFunc("Fiona",
);
}
Liam is 2 years old.
Fiona is 20 years old.

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt