Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Instruction Return dans les Fonctions | Introduction aux Fonctions
Introduction Au C++

bookInstruction Return dans les Fonctions

L'instruction return termine l'exécution d'une fonction et renvoie une valeur d'un type prédéfini.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

Si le type est spécifié de manière incorrecte, la fonction aura un comportement imprévisible.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

Autrement dit, avant de créer une fonction, le type de données qu'elle retourne doit être spécifié. De plus, en C++, il existe des fonctions void spéciales. Les fonctions de ce type de données sont autorisées à ne rien retourner :

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

Il peut y avoir plusieurs instructions return à l'intérieur des fonctions, et chacune ne sera exécutée que sous certaines conditions.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

S'il y a deux instructions return, la seconde sera ignorée :

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

Que se passe-t-il lorsqu'une instruction return est exécutée à l'intérieur d'une fonction ?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 3

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

Awesome!

Completion rate improved to 3.85

bookInstruction Return dans les Fonctions

Glissez pour afficher le menu

L'instruction return termine l'exécution d'une fonction et renvoie une valeur d'un type prédéfini.

function.h

function.h

copy
12345
int func() // int - predefined { int variable = 10; return variable; // variable = 10 }

Si le type est spécifié de manière incorrecte, la fonction aura un comportement imprévisible.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }

Autrement dit, avant de créer une fonction, le type de données qu'elle retourne doit être spécifié. De plus, en C++, il existe des fonctions void spéciales. Les fonctions de ce type de données sont autorisées à ne rien retourner :

first_example.cpp

first_example.cpp

second_example.cpp

second_example.cpp

copy
123456789101112
#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }

Il peut y avoir plusieurs instructions return à l'intérieur des fonctions, et chacune ne sera exécutée que sous certaines conditions.

main.cpp

main.cpp

copy
1234567891011121314151617
#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }

S'il y a deux instructions return, la seconde sera ignorée :

main.cpp

main.cpp

copy
123456789101112131415
#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
question mark

Que se passe-t-il lorsqu'une instruction return est exécutée à l'intérieur d'une fonction ?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 3
some-alt