Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Instrução Return em Funções | Introdução às Funções
Introdução ao C++

bookInstruçã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

function.h

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

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

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

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

Podem existir múltiplos retornos dentro de funções, e cada um será executado apenas sob certas condições.

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 }

Se houver dois retornos, o segundo retorno da função será ignorado:

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

O que acontece quando uma declaração return é executada dentro de uma função?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 3.85

bookInstruçã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

function.h

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

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

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

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

Podem existir múltiplos retornos dentro de funções, e cada um será executado apenas sob certas condições.

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 }

Se houver dois retornos, o segundo retorno da função será ignorado:

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

O que acontece quando uma declaração return é executada dentro de uma função?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 3
some-alt