return statement — cppreference.com
De cppreference.com
<metanoindex/>
Met fin à la fonction en cours et retourne la valeur spécifiée pour la fonction appelante .
Original:
Terminates current function and returns specified value to the caller function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Syntaxe
return expression
|
(1) | ||||||||
return
|
(2) | ||||||||
Explication
La première version évalue la expression, met fin à la fonction en cours et renvoie le résultat de la expression à la fonction. Le type résultant de l'expression doit être convertible à fonctionner type de retour .
Original:
The first version evaluates the expression, terminates the current function and returns the result of the expression to the caller function. The resulting type of the expression must be convertible to function return type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
La deuxième version met fin à la fonction en cours. Valable uniquement si le type de retour de la fonction est void .
Original:
The second version terminates the current function. Only valid if the function return type is void.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Mots-clés
Exemple
#include <iostream> void fa(int i) { if (i == 2) return; std::cout << i << '\n'; } int fb(int i) { if (i > 4) return 4; std::cout << i << '\n'; return 2; } int main() { fa(2); fa(1); int i = fb(5); i = fb(i); std::cout << i << '\n'; }
Résultat :