Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Sentencia Return en Funciones | Introducción a las Funciones
Introducción a C++

bookSentencia Return en Funciones

La sentencia return termina la ejecución de una función y devuelve un valor de un tipo predefinido.

function.h

function.h

copy
12345
int 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

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

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

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(); }

Puede haber múltiples sentencias de retorno dentro de las funciones, y cada una se ejecutará solo bajo ciertas condiciones.

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 }

Si hay dos sentencias de retorno, la segunda será ignorada:

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

¿Qué sucede cuando se ejecuta una sentencia return dentro de una función?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain what a void function is in more detail?

What happens if a function doesn't have a return statement?

Can you give an example of a function with multiple return statements?

Awesome!

Completion rate improved to 3.85

bookSentencia 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

function.h

copy
12345
int 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

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

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

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(); }

Puede haber múltiples sentencias de retorno dentro de las funciones, y cada una se ejecutará solo bajo ciertas condiciones.

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 }

Si hay dos sentencias de retorno, la segunda será ignorada:

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

¿Qué sucede cuando se ejecuta una sentencia return dentro de una función?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
some-alt