Instruction Return dans les Fonctions
L'instruction de retour termine l'exécution d'une fonction et renvoie une valeur d'un type prédéfini.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Si le type est spécifié de manière incorrecte, la fonction se comportera de manière imprévisible.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } //The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
C'est-à-dire, 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. Les fonctions de ce type de données peuvent ne rien retourner:
first_example.cpp
second_example.cpp
12345678910111213#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 dans les fonctions, et chacune ne s'exécutera que sous certaines conditions.
main.cpp
123456789101112131415161718192021#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 return, le second sera ignoré:
main.cpp
12345678910111213141516#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 4
Instruction Return dans les Fonctions
Glissez pour afficher le menu
L'instruction de retour termine l'exécution d'une fonction et renvoie une valeur d'un type prédéfini.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Si le type est spécifié de manière incorrecte, la fonction se comportera de manière imprévisible.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } //The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
C'est-à-dire, 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. Les fonctions de ce type de données peuvent ne rien retourner:
first_example.cpp
second_example.cpp
12345678910111213#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 dans les fonctions, et chacune ne s'exécutera que sous certaines conditions.
main.cpp
123456789101112131415161718192021#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 return, le second sera ignoré:
main.cpp
12345678910111213141516#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }
Merci pour vos commentaires !