This rule may apply in C, but it can be considered obsolete in C++ because of exceptions. As soon as your function throws an exception or calls a function that can throw, you have an additional exit point:
int f()
{
//...
g(); // g() may throw: you have an exit point here
//...
throw exc; // another possible exit point
//...
return returnValue; // Nice try, but you have additional exit points
}
This in addition to the point made in other answers: this rule intends to make the code easier to follow, but it is easy to find examples where this is not true. Much better:
if (condition)
return a;
if (condition2)
return b;
if (condition3)
return c;
// Insert all your code for the general case
than:
int returnValue;
if (!condition) {
if (!condition2) {
if (!condition3) {
// Insert your code here
}
else {
returnValue = c;
}
returnValue = b; // Where am I now?
}
returnValue = a;
}
return returnValue;
And then you also have the case when you decide the return value in a switch
:
switch (a)
{
case 1: return 10;
case 2: return 20;
case 3: return 40;
default: return 50;
}
rather than:
int returnValue;
switch (a)
{
case 1: returnValue = 10; break;
case 2: returnValue = 20; break;
case 3: returnValue = 40; break;
default: returnValue = 50; break;
}
return returnValue; // Where is the clarity gained?