Using return inside of switch case statement inside of function

Okay so i have the following code and i was wondering if i will have any problems returning from the function without breaking from the switch statement?

switch(I2Cstatus){
		case 0:
			Serial.print(F("\""));
			Serial.print(valToSend);
			Serial.print(F("\" Successfully sent to I2C address: "));
			Serial.println(address);
			DEBUG_PRINTLN(F("End sendI2C()\n"));
			return 1;
		case 1:
			Serial.println(F("Error - Data too long to fit in transmit buffer"));
			DEBUG_PRINTLN(F("End sendI2C()\n"));
			return 0;

		case 2:
			Serial.println(F("Error - Received NACK on transmit of address"));
			DEBUG_PRINTLN(F("End sendI2C()\n"));
			return 0;
		case 3:
			Serial.println(F("Error - Received NACK on transmit of data"));
			DEBUG_PRINTLN(F("End sendI2C()\n"));
			return 0;
		case 4:
			Serial.println(F("Error - Unknown"));
			DEBUG_PRINTLN(F("End sendI2C()\n"));
			return 0;
		default:
			DEBUG_PRINTLN(F("I2Cstatus Value > 4 or < 0"));
			return 0;
	}

No problem, you can return from a function ANYWHERE within that function. The middle of a loop, or a switch statement, makes no odds. return will return.

The breaks used in switch/case prevent execution of the code from falling through to the next case but your returns will do the same. Why not put the breaks in if you are worried about them not being there, or alternatively set a variable in each case and return that at the end of the function ?

That use of return is allowed, but why? I prefer to write code so that there is ONE return statement. I do that so I can put entry and exit statements in the function:

int SomeClass::DoesSomething(int someValue)
{
   cout << "SomeClass::DoesSomething ==>" << endl;
   int retVal = 0;

   // do something
   cout << "SomeClass::DoesSomething <==" << endl;
   return retVal;
}

This way, if the function is called in a try block, I can tell the difference between the function having reached the end and the function having thrown an exception.

Obviously, this isn’t on the Arduino, where exception handling isn’t supported (for obvious reasons), but, it’s a good habit to develop.

Both Paul and Bob are giving you sage advice. A single exit point from a function is almost always preferable to multiple exit points. It makes reading, testing, and debugging the code easier. Also, during testing, if you used "forced" values from the function, a single return point makes it easy to vary the forced test values.

econjack:
Both Paul and Bob are giving you sage advice. A single exit point from a function is almost always preferable to multiple exit points. It makes reading, testing, and debugging the code easier. Also, during testing, if you used “forced” values from the function, a single return point makes it easy to vary the forced test values.

I’m adding my support for this statement. One day (years from now) someone may have to debug your code. That someone could be you.

Jesse

I disagree with that statement as a blanket rule. In some cases, there is a series of actions required that either pass or fail, and a return value is required to denote the pass or fail to the calling function.

Here is an example of a series of actions taken, and a return value that denotes pass (return 1) or fail (return 0). http://playground.arduino.cc/Code/Email

There is probably a way to rewrite the code to have one return in that code, but it would be much more difficult for me to understand, and to bypass the code sections that depend on previous actions.