Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Spørg mig spørgsmål om dette emne

Opsummér dette kapitel

Vis virkelige eksempler

Awesome!

Completion rate improved to 2.94

bookParameters

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
some-alt