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á 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
12345678910111213
#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 determinadas condições.

main.cpp

main.cpp

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

Se houver dois retornos, a segunda instrução de retorno será ignorada:

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }

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

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

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á 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
12345678910111213
#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 determinadas condições.

main.cpp

main.cpp

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

Se houver dois retornos, a segunda instrução de retorno será ignorada:

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> int func() { int a = 50; int b = 6; return a; return b; } int main() { std::cout << func() << std::endl; //func calling }

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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