[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/return [Back]  [Original]

return statement cppreference.com
cppreference.com
Espaces de noms
Variantes

return statement

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
Met fin la fonction en cours et retourne la valeur spcifie 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 premire version value la expression, met fin la fonction en cours et renvoie le rsultat de la expression la fonction. Le type rsultant 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 deuxime 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-cls

return

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';
}

Rsultat :

1
4
2

Web Proxy Viewer  |  New URL  |  Original Page