# return in switch statement within a string returning method...

• 06-30-2008
ozyabm
return in switch statement within a string returning method...
Hi,

Here is a method inside a string returning function:

Code:

```string computeSin(argument 1, argument 2) { if ( strcmp(icmop->icmo_addl_infop->retval_str, "USA") == 0 ) { string newIsin = (string)"US" + icmop->icmo_tranche_cusips[trancheNum]; int d1, d2, sum, multiply, i; for (sum = 0, multiply = 1, i = 10; i > -1; --i) { switch (i) { case 0: case 1: if (isupper(newIsin[i])) d1 = newIsin[i] - 'A' + 10; else return 0; break; default: if (isupper(newIsin[i])) d1 = newIsin[i] - 'A' + 10; else if (isdigit(newIsin[i])) d1 = newIsin[i] - '0'; else return 0; break; } if (d1 < 10) { d1 *= (multiply ? 2 : 1); multiply = !multiply; } else { d2 = d1 / 10; d1 %= 10; d1 *= (multiply ? 2 : 1); d2 *= (multiply ? 1 : 2); sum += (d2 % 10) + (d2 / 10); } sum += (d1 % 10) + (d1 / 10); } sum %= 10; sum = 10 - sum; sum %= 10; std::stringstream isinSs; isinSs << newIsin << sum; const std::string &checkedIsin = isinSs.str(); return checkedIsin; } return argument 2; }//end of string method```
My Question:
If in the switch statement section of this code I reach return 0 (appears twice: once in case 1 and other in default), would I be taken out of this computeSin method or would I keep looping till the for loop finishes?

Normally, a return statement takes you out of a function but in this case if I reach return 0 which is in a switch statement that will keep running till the for loop ends, would I exit the whole method?

Thanks.
• 06-30-2008
laserlight
The use of a switch has no effect on the usual properties of a return statement.
• 06-30-2008
Daved
Only break applies to the switch, return applies to the function.

BTW, I would use string("US") instead of (string)"US" and consider the string's == operator instead of strcmp.
• 06-30-2008
ozyabm
Thanks for the replies.

I will make those changes and compile to see results.

I understand that break applies to switch and return applies to the function but if I reach a return within a switch clause, would I exit the function and return a 0 as my result to calling computeIsin()?
• 06-30-2008
Daved
>> would I exit the function and return a 0 as my result to calling computeIsin()?
Yes, just as would happen with a return 0 anywhere else in the function.
• 06-30-2008
ozyabm
Thanks a lot everyone. That really helped.