Multiple return statements

Viewed 266

I am currently reading "A tour of C++" from Bjarne Stroustup, and I saw the following example:

bool accept()
{
     cout << "Do you want to proceed (y or n)?\n";     // write question
     char answer = 0;                                  // initialize to a value that will not appear on input
     cin >> answer;                                    // read answer

     if (answer == 'y')
           return true;
     return false;
}

I thought using multiple return statements wasn't recommended. Wouldn't it be better practice in this case to create a bool variable, initialize it to 0 and then modify it in the if condition, to finally return the value of this boolean variable? Or I am just making things up.

2 Answers

No, you're not entirely making things up, and no there is no recommendation of such that I know of.

Snippet According to your recommendation:

Int accept()
{
     Int result=0;
     cout << "Do you want to proceed (y or n)?\n";     // write question
     char answer = 0;                                  // initialize to a value that will not appear on input
     cin >> answer;                                    // read answer

     if (answer != 'y')
         result=1;

     return result;
}

Looking closer you will notice no difference between the snippet above and the one you found in the book, they book use validation, the difference is the data-type used. Now, it will be more easy to use a boolean if you need to simple use the function accept like below:

if(accept()) /*do something*/;

Rather than:

if(accept()==0) /*do something*/

Either way, you're good to go.

Another thing to consider is the size of your data-type, boolean is just 1bit while int is 4bytes so comparison time complexity will obviously be different but you will most likely never notice the difference.

Edit: In java boolean is 1bit as said above but since it's c++, because of the issue of 1bit not addressable, it only make more sense to make it 8bit and then it can be given an address in the memory. So, in C++ boolean is also 1byte or 8bit, and so the comparison time difference is not a thing to consider.

Both approaches are possible, but you will usually see the version using multiple return values. Take a look how this part of the code works:

     if (answer == 'y')
           return true;
     return false;

At first you may see two branches in which the executed code can go, based on the value of answer. First one is connected with y choice, and the other one with anything else. This code could also look like this:

     if (answer == 'y')
           return true;
     else
           return false;

But it makes the program a bit more complicated - there is one more jmp instruction in assembly code, which means that the code is just a bit more complex, but its functionality is exactly the same as of the previous version.

Now, we can add the bool variable and let's see how it will look like:

     bool is_answer_positive = false;
     if (answer == 'y')
           is_answer_positive = true;
     else                              // we can get rid of this statement
           is_answer_positive = false; // as we have defined variable earlier

     return is_answer_positive;

The first thing which makes the code a bit more complex is a new variable, which does not add any vital value to the code, I would say it makes it only more complicated. This is the first instruction which makes the code bigger, but not better. Then if/else works similarly to the second version, but you also assign some values to the is_answer_positive, which also makes the code bigger, but not better. At the end, you return the value you got from if/else statement and the code is done. We can get rid of else statement, as is_answer_positive was initialized to be false, but this is still not the best solution.

Comparing those three possibilities: first version is the most used one, it is clear and probably the simplest, second version adds extra jump, which is unnecessary at all, and the third version adds extra variable, which makes the code a bit harder to read.

Related