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

Contenido del Curso

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

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) { &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

¿Todo estuvo claro?

Sección 4. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt