Instrução Return em Funções
A instrução return encerra a execução de uma função e retorna um valor de um tipo predefinido.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Se o tipo for especificado incorretamente, a função apresentará um comportamento imprevisível.
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; }
Ou seja, antes de criar uma função, o tipo de dado que ela retorna deve ser especificado. Além disso, em C++, existem funções especiais do tipo void. Funções desse tipo de dado podem não retornar nenhum 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(); }
Podem existir múltiplos retornos dentro de funções, e cada um será executado apenas sob certas condições.
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 }
Se houver dois retornos, o segundo retorno da função será ignorado:
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 }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 3.85
Instrução Return em Funções
Deslize para mostrar o menu
A instrução return encerra a execução de uma função e retorna um valor de um tipo predefinido.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Se o tipo for especificado incorretamente, a função apresentará um comportamento imprevisível.
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; }
Ou seja, antes de criar uma função, o tipo de dado que ela retorna deve ser especificado. Além disso, em C++, existem funções especiais do tipo void. Funções desse tipo de dado podem não retornar nenhum 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(); }
Podem existir múltiplos retornos dentro de funções, e cada um será executado apenas sob certas condições.
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 }
Se houver dois retornos, o segundo retorno da função será ignorado:
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 }
Obrigado pelo seu feedback!