Sentencia Return en Funciones
La sentencia return termina la ejecución de una función y devuelve un valor de un tipo predefinido.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Si el tipo se especifica incorrectamente, la función se comportará de manera impredecible.
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; }
Es decir, antes de crear una función, se debe especificar el tipo de dato que retorna. Además, en C++ existen funciones especiales denominadas funciones void. Las funciones de este tipo de dato pueden no retornar ningún valor:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
Puede haber múltiples sentencias de retorno dentro de las funciones, y cada una se ejecutará solo bajo ciertas condiciones.
main.cpp
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 }
Si hay dos sentencias de retorno, la segunda será ignorada:
main.cpp
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 }
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 3.85
Sentencia Return en Funciones
Desliza para mostrar el menú
La sentencia return termina la ejecución de una función y devuelve un valor de un tipo predefinido.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Si el tipo se especifica incorrectamente, la función se comportará de manera impredecible.
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; }
Es decir, antes de crear una función, se debe especificar el tipo de dato que retorna. Además, en C++ existen funciones especiales denominadas funciones void. Las funciones de este tipo de dato pueden no retornar ningún valor:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
Puede haber múltiples sentencias de retorno dentro de las funciones, y cada una se ejecutará solo bajo ciertas condiciones.
main.cpp
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 }
Si hay dos sentencias de retorno, la segunda será ignorada:
main.cpp
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 }
¡Gracias por tus comentarios!