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

bookBasics of Functions

The function is a block of statements that runs when it is called. The world of programming cannot exist without functions since they:

  • are reusable.
  • can test a particular part of your code.
  • can be applied to a number of cases if you change the parameters.

Let’s go deep into it. For instance, you have decided to program the cat. It should say "meow!":

cout << "meow!" << endl;

Easy implementation. Repeating output will overload your code. However, what should we do if the cat must say meow twice or 20 times in different periods? In such a situation, we can use functions. To create a function, you need to specify the keyword void, the name of the function, followed by parentheses ():

void myFunc() {
    // code to be executed
}

Let's create the function sound() for the cat, which will print "meow!":

void sound() {
    cout << "meow!" << endl;
}
question mark

To create a function, you need to specify the keyword

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Posez-moi des questions sur ce sujet

Résumer ce chapitre

Afficher des exemples du monde réel

Awesome!

Completion rate improved to 2.94

bookBasics of Functions

Glissez pour afficher le menu

The function is a block of statements that runs when it is called. The world of programming cannot exist without functions since they:

  • are reusable.
  • can test a particular part of your code.
  • can be applied to a number of cases if you change the parameters.

Let’s go deep into it. For instance, you have decided to program the cat. It should say "meow!":

cout << "meow!" << endl;

Easy implementation. Repeating output will overload your code. However, what should we do if the cat must say meow twice or 20 times in different periods? In such a situation, we can use functions. To create a function, you need to specify the keyword void, the name of the function, followed by parentheses ():

void myFunc() {
    // code to be executed
}

Let's create the function sound() for the cat, which will print "meow!":

void sound() {
    cout << "meow!" << endl;
}
question mark

To create a function, you need to specify the keyword

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1
some-alt