Functions · Fonctions
A FUNCTION returns a value
- A function works something out and hands back a value with
RETURN. RETURNS(in the header) states the type of value it gives back.
A FUNCTION retourne une valeur
- Une fonction calcule quelque chose et rend une valeur avec
RETURN. RETURNS(dans l'en-tête) indique le type de valeur qu'elle retourne.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6) // → 36
Run, then RETURN a value · Exécuter, puis RETURN une valeur
A FUNCTION runs its steps, then RETURNs a value you can use in an expression. · Un FUNCTION exécute ses étapes, puis RETURNs une valeur que vous pouvez utiliser dans une expression.
Use the returned value
- Call a function inside an expression, or store its result in a variable.
Utiliser la valeur retournée
- Appelez une fonction à l'intérieur d'une expression, ou stockez son résultat dans une variable.
FUNCTION Square(Number : INTEGER) RETURNS INTEGER
RETURN Number * Number
ENDFUNCTION
DECLARE Answer : INTEGER
Answer ← Square(4)
OUTPUT Answer + 1 // → 17
A function can return a Boolean
- The result can be a
BOOLEAN— handy for a yes/no test.
Une fonction peut retourner un Booléen
- Le résultat peut être un
BOOLEAN— pratique pour un oui/non.
FUNCTION IsPositive(N : INTEGER) RETURNS BOOLEAN
RETURN N > 0
ENDFUNCTION
OUTPUT IsPositive(5) // → TRUE
Watch out
- A procedure does something; a function returns a value you then use.
- Every path through a function must reach a
RETURN.
Attention
- Une procédure fait quelque chose ; une fonction retourne une valeur que vous utilisez ensuite.
- Tout chemin à travers une fonction doit atteindre un
RETURN.
Common mistakes
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- A function must
RETURNa value.
Erreurs courantes
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- Une fonction doit
RETURNune valeur.
Now you try
- Define a function with
RETURNSandRETURN. - Press Check answer to test it.
À vous maintenant
- Définir une fonction avec
RETURNSetRETURN. - Appuyez sur Vérifier la réponse pour tester.
Define FUNCTION Square(N : INTEGER) RETURNS INTEGER that returns N × N, then output Square(5) (it is 25). · Définir FUNCTION Square(N : INTEGER) RETURNS INTEGER qui retourne N × N, puis afficher Square(5) (c'est 25).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Define FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER that returns a + b, then output Add(3, 4) (it is 7). · Définir FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER qui retourne a + b, puis afficher Add(3, 4) (c'est 7).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Define FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN that returns TRUE when N is even, then output IsEven(8) (it is TRUE). · Définir FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN qui retourne TRUE si N est pair, puis afficher IsEven(8) (c'est TRUE).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.