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

Зміст курсу

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

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.

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 3
some-alt