-3

I am currently self-learning C and C++.
I couldn't find a similar question but I would like some good explanation with a focus on C and C++ design practices.

  1. Is it a good coding practice to call a function with a return type from return()?
  2. Is this something I should even be thinking of or is this a silly question to ask?

To illustrate:

bool A (blah, blah)
{
    bool a = false;
    a = B(); // B returns bool type
    return a;

    // OR

    return (B());
}
  1. Both compile and run obviously but is one better than the other in terms of some metric such as speed, memory efficient etc..?
  2. What could be the reasons to use one or the other or am I wasting my time by asking this question?
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Papa Delta
  • 267
  • 3
  • 12
  • 4
    there is no language called C/C++ – 463035818_is_not_an_ai Aug 28 '18 at 21:51
  • 4
    Just `return B();` is all you need. There isn't any reason to store the return in a local variable if you are just going to return it. – NathanOliver Aug 28 '18 at 21:51
  • 2
    Any decent compiler will optimize them to the same code (at least when optimizations are on). The former style is usually seen in people that prefer 1 return statement in a function while the latter is, in my opinion, clearer, more succinct and far less to type. – Nathan Ernst Aug 28 '18 at 21:51
  • 1
    Also, if do not already have one I suggest you get a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Aug 28 '18 at 21:51
  • 1
    Go for the one that is easiest to read and hardest to get wrong. For me that's `return B();` – user4581301 Aug 28 '18 at 21:51
  • 1
    There will be no significant difference. Choose the one that expresses your intent the clearest and that you will easily understand if you come back to the code in 2 years time. – Richard Critten Aug 28 '18 at 21:53
  • 3
    The parens around the return expression is superfluous, and I recommend against using those superfluous parens unless there is a reason to use them in a particular case. Keyword `return` is not a function. – Eljay Aug 28 '18 at 21:53
  • 4
    NOTABENE : When a function has trailing return type speciifcation of decltype(auto) then using parenthesis in the return statement will return a reference instead of a value. UB lurking ! – engf-010 Aug 28 '18 at 21:58
  • 7
    Decide on learning one of C++ or C. –  Aug 28 '18 at 22:01
  • One other consideration that might apply. Assigning the value to a local variable makes it easier to inspect that value while debugging, because the function call / assignment and the return are on separate lines. Typically you'd step over `a = B();`, look at the value of `a`, and if it is unexpected, you can set the call to `B()` as the next statement, then trace into it, to find out what's going on. – Tim Randall Aug 30 '18 at 13:36
  • Write your code for maintainability. When you profile your code, and determine that you have to change an implementation to improve the performance at the cost of legibility of that function, add a big comment as why the function was intentionally butchered -- perhaps keeping the "readable but inefficient" routine for posterity and unit tests as a reference routine. – Eljay Aug 30 '18 at 14:25

7 Answers7

5

If you plan on doing something with the return value of B before returning from A, go ahead and store it. If the return value of B will be the return value of A, then just return B().

dbush
  • 205,898
  • 23
  • 218
  • 273
2

You can return just B() but, the class does not make any sense if you put only this statement in A class. In that case, call B() instead of A() and remove A()

xzegga
  • 3,051
  • 3
  • 25
  • 45
2

The compiler will (probably) optimize your bool a = B(); return a; into return B() anyways -- so you're wasting your time. The compiler may even just place B() in places where you wrote A().

Don't spend too much time optimizing unless you have (or expect to have) a problem. Keep a focus on making it work correctly and keeping the code clear.

It's quite common (and perfectly fine) to return the result from other function.

João Neto
  • 1,732
  • 17
  • 28
  • 1
    Playing devil's advocate for a moment, the one advantage of separating this into `bool a = B();` `return a;` is that it makes it much easier when single stepping though a debug non-optimized build to see what `B()` actually returned. And as you've noted, any half way decent optimizing compiler will compress those two statements to one for an optimized release build. – dgnuff Aug 28 '18 at 22:53
1

Both methods are common and it is a matter of preferred coding style.

There will be no significant speed difference between them.

As dbush mentioned one reason to use the first method is if you need to do something with the result of B() before returning it.

codeandkey
  • 69
  • 5
1

This is going to be the least of your problems and will break down to personal choice. A good compiler will be able to optimize in such a way that there is not going to be a difference (in most cases).

However, C++17 guarantees for the second case Return-Value-Optimization which could in some cases be better.

Return-Value-Optimization (RVO) is a process, where the returned object is never stored in a temporary object.

So how should you decide:

If you want to make use of the result of B() within the scope of A, than store it. Otherwise directly write return B();.

1

There is no difference in terms of code. The compiler should be able to optimize these to the same thing when building a release build.

But there is a slight advantage to using a variable in debug build. You can easily place a break point at the return then query the state of the variable before returning.

bool A (blah, blah)
{
    bool a = B();
    return a;        // Set break point here.
}                    // In debugger you can see the value of a
                     // before telling the compiler to continue.
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

Well, there are bad points for both:

  1. The first is unnecessarily chatty. If in doubt, prefer brevity.
    In C or C++11, that's it, in C++17 it looses out on guaranteed copy-elision, which might matter with a more complicated type.
  2. The second uses extra-parentheses, making it look like return is a function-call.
    In C or C++11, that's it, but in C++14 with decltype(auto) return-type-deduction, that results in deducing references.

    // C++14 examples:
    decltype(auto) returns_value(int i) { return i; }
    decltype(auto) returns_dangling_reference(int i) { return (i); }
    

In the end, it should have been simply:

return B();

So you get all the advantages and avoid all the pitfalls.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Can you give an example on how this leads to problems in C++14 due to the return-type deduction? – João Neto Aug 28 '18 at 22:09
  • 1
    @joaomineto: When a function has trailing return type speciifcation of decltype(auto) then using parenthesis in the return statement will return a reference instead of a value. That is most likely not what you want and easily leads to UB (references to locals or temporaries). – engf-010 Aug 28 '18 at 22:13