Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Is it useful to use returns in the middle of functions? C++

I have a question to ask to which I have not found an explanation for a long time. My teachers keep telling me that return statements in the middle of functions should not be included without ever telling me why. I am mainly referring to the programming languages: C, C++, C# and Java.

The same goes for break statements.

Here is an example of code that is deemed invalid because of the return statement.

void Stack::Extract()
{
    if (Times == 0) 
    {
        cout << "The stack is empty, no number can be drawn" << endl;
        return;
    }

    cout << "Extracted value: " << Vet[Counter-1] << endl;
    Vet[Counter-1] = 0;
    Times--;
    Counter = Times;
}

This is the function for extracting a value from a stack (done via an array), a simple exercise that has been assigned to the class. I'm here asking for information because if return is not to be used in the middle of functions I need to know why.

In this case I have saved the declaration of temporary variables and concatenation of if / else structures as well as preventing the execution of the entire function code if not needed.

EDIT for "Needs details or clarity": I am reading all the comments and I thank you for that. My question is simply what I wrote so in my opinion it doesn't need details or clarity.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Having returns inside your functions makes life much easier.

if (x == 0) return;
do other stuff..

Code gets much more readability. I guess, your teacher has a weird approach to this point. My tipp: Follow his rules, as long as you have to. Afterwards do it like 99% of the industry.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda