Domanda

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...

È stato utile?

Soluzione

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):

Altri suggerimenti

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.

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

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++.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top