Functions

Giving a piece of program a name

Donner un nom à un morceau de programme

A function is a piece of program with a name. You write it once with function, then you call it as often as you like, by writing its name followed by brackets.

Une fonction est un morceau de programme qui porte un nom. On l’écrit une fois avec function, puis on l’appelle autant de fois qu’on veut, en écrivant son nom suivi de parenthèses.

What you put between the brackets when you call it (here "Ada") arrives in the parameter name. You already know a function: console.log!

Ce qu’on met entre les parenthèses en l’appelant (ici "Ada") arrive dans le paramètre prenom. Tu connais déjà une fonction : console.log!

Giving back an answer

Renvoyer une réponse

return gives a value back to the program, which can put it in a variable, use it in a calculation or give it to another function. console.log, on the other hand, only shows a value on the screen.

return renvoie une valeur au programme, qui peut la ranger dans une variable, s’en servir dans un calcul ou la donner à une autre fonction. console.log, lui, ne fait que montrer une valeur à l’écran.

As soon as return runs, the function stops: whatever comes after it is not read.

Dès que return est exécuté, la fonction s’arrête : la suite n’est pas lue.

Several parameters, and the Math functions

Plusieurs paramètres, et les fonctions de Math

A function can have several parameters, separated by commas. JavaScript also comes with ready-made functions for maths, whose names start with Math.

Une fonction peut avoir plusieurs paramètres, séparés par des virgules. JavaScript a aussi des fonctions toutes faites pour les maths, dont le nom commence par Math.

17 = 3 × 5 + 2: Math.floor(17 / 5) gives the quotient 3, and 17 % 5 the remainder 2. That is exactly one step of Euclid’s algorithm.

17 = 3 × 5 + 2 : Math.floor(17 / 5) donne le quotient 3, et 17 % 5 le reste 2. C’est exactement une étape de l’algorithme d’Euclide.

Exercises

Exercices

Here, ✓ Check calls your function with several values and compares what it returns with the right answer. So use return, not console.log. You can still add console.log lines under the function to try it with ▶ Run.

Ici, ✓ Vérifier appelle ta fonction avec plusieurs valeurs et compare ce qu’elle renvoie avec la bonne réponse. Utilise donc return, pas console.log. Tu peux quand même ajouter des lignes console.log sous la fonction pour l’essayer avec ▶ Exécuter.

The square

Le carré

Write a function square that receives a number x and returns x × x.

Écris une fonction carre qui reçoit un nombre x et renvoie x × x.

Even?

Pair?

Write a function isEven that returns true if n is even, and false otherwise.

Écris une fonction estPair qui renvoie true si n est pair, et false sinon.

The bigger of two

Le plus grand des deux

Write a function bigger(a, b) that returns the bigger of the two numbers, without using Math.max.

Écris une fonction plusGrand(a, b) qui renvoie le plus grand des deux nombres, sans utiliser Math.max.

The quotient

Le quotient

In a division, a = quotient × b + remainder. Write a function quotient(a, b) that returns the quotient, a whole number. For example, quotient(17, 5) is 3.

Dans une division, a = quotient × b + reste. Écris une fonction quotient(a, b) qui renvoie le quotient, un nombre entier. Par exemple, quotient(17, 5) vaut 3.