C/C++ conditional return statements [duplicate]

断了今生、忘了曾经 提交于 2019-11-29 19:06:25

问题


I am working on embedded program and in certain cases if a condition is not meant, I would like to return from function as quickly as possible. if I have the following code and I am doing embedded programming:

foo() {
   if (a < b) {
       return 0;  // bail, since condition is met
   } else {
       // lots of calculations in this block
   }
   return 1;
}

My question is, is it bad having multiple return statements? Is it bad practice? Are there better methods? Does MISRA say anything about it?

NOTE: This question is particular to Embedded Systems, has to do with MISRA not just C/C++

Thanks...


回答1:


MISRA requires a single return statement:

(MISRA, rule 14.7 : required) "A function shall have a single point of exit at the end of the function"

Now, personally I don't think it is a good rule. Minimize the number of return statements but use a return statement when it enhances the readability of your code.

For example guard clauses can make your code cleaner and more readable.

I suggest you to read this article about duffing (writing code from top to bottom):




回答2:


I would have written that like so because the else is redundant:

   if (a < b) {
       return 0;  // bail, since condition is met
   }
   // lots of calculations in this block
   return 1;

I don't think there's a rule of thumb, but if the function is really long and you have multiple return points, it can be hard to maintain and understand.

However, in a recursive function, for example, it's very handy to put your "base cases" as return statements at the beginning of a function.

For example, consider factorial:

int fact(int x) {
  // base cases
  if (x == 0 || x == 1)
    return 1;

  // recursive call
  return x * fact(x-1);
}

You could also write it like this:

int fact(int x) {
  int ret = 0;
  if (x == 0 || x == 1)
    ret = 1;
  else
    ret = x * fact(x-1);
  return ret;
}

I just like the first way better, but it doesn't mean either way is better than the other.

It really comes down to whatever standards you must follow and personal preference.




回答3:


Some consider it bad practice, I'm personally ok with it as it can be cleaner-looking code. You can also do it in this way:

foo() {
   int results = 0;  // good practice to initialize
   if (a < b) {
       results = 0;  // redundant, likely optimized out
   } else {
       // lots of calculations in this block
       results = 1;
   }
   return results;
}



回答4:


Multiple return statements is perfectly valid in C/C++. But make sure that at least one of the return statements is always executed. The following function is incorrect,

int foo(int x)
{
  if(x>0)
  {
    return 1;
  }
  else if(x==0)
  {
    return 0;
  }
  else
  {
    // No return in this block
  }
}

such situations should be avoided in C/C++.




回答5:


Having multiple return statements in a function is perfectly acceptable. In fact, having multiple return statements in a function like the one listed above can provide performance and readability improvements. For example, you don't need the else block in the above function because you return from the function if the condition is met.

Just make sure that if your function does not have a return type of void, then you have a return statement at the end, and that all return statements return that type. For example, if your function is declared like this:

int foo ();

Then all your return statements must return integers, and you should return an integer at the end, no matter what. But if your function has a return type of void, like this:

void foo ();

Then if you reach the end of the function and have no return keyword, the function will return automatically.



来源:https://stackoverflow.com/questions/17177276/c-c-conditional-return-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!