Return vs return functions?

To return or not to return, this is a question for functions! Or, does it really matter?


Here is the story : I used to write code as follows:

Type3 myFunc(Type1 input1, Type2 input2){}

But recently, my project colleges told me that I should try as little as possible to avoid writing such a function, and suggest the next way by putting the return value in the input parameters.

void myFunc(Type1 input1, Type2 input2, Type3 &output){}

They convinced me that it’s better and faster due to the extra copy step when returning in the first method.


For me, I am beginning to believe that the second way is better in some situations, especially I have a few things that need to be returned or changed. For example: the second line of the next will be better and faster than the first, which avoids copying everything vecor<int>when you return.

vector<int> addTwoVectors(vector<int> a, vector<int> b){}
void addTwoVectors(vector<int> a, vector<int> b, vector<int> &result){}:

But in some other situations I can’t buy it. For example,

bool checkInArray(int value, vector<int> arr){}

will definitely be better than

void checkInArray(int value, vector<int> arr, bool &inOrNot){}

In this case, I think the first method directly returning the result is better in terms of better readability.


So, I'm confused (focused on C ++):

  • What should be returned by functions and what should not (or try to avoid)?
  • Is there any standard way or good suggestions for me?
  • Can we work better in both readability and code efficiency?

Edit: , . , return-type functions, method chaining. , , , .

, , . , , C, C++ .. , ( ).

+4
7

, - , , , ? , ? , , .

, , , , : , , , . , , , , .. . , , . , (, , , - ), .

. # Java ( , Java , ), C , , , ( , ). ++ , RVO, , , A a = f(b);, f . ++ 11 , .

A* ? . , std::shared_ptr<A> std::unique_ptr<A>.

, , , , . , , , : , , , . , , ; , . , ; , , - .

. , . , . , , , , - , .

+4

( ) , .

:

return_value foo( void );

, :

  • (RVO)
  • (NRVO)

? :

return_value foo( void ) { return return_value(); }

. - ( ) :

void call_foo( void )
{
    return_value tmp = foo();
}

tmp foo, foo , . , return_value .

RVO? , . , , .

?

; , l-. , , , :

return_type foo( void )
{
    return_type bar;
    // do stuff
    return bar;
}

, - , , ; :

return_type foo( void)
{
    if(some_condition)
    {
        return_type bar = value;
        return bar;
    }
    else
    {
        return_type bar2 = val2;
        return bar2;
    }
}

. - , NRVO , . , return_value , , .

NRVO , - ; , .

, return.

++ 11 ++ 03 ; , , , - . ++ 03 boost.move, - .

, RVO, , . ++ 11, , STL STD- . / (MSVC ), : copy-and-swap, !

" " ?

, return_value , RVO, , , , , .

!

, float int bool, - ; - ; - , . - bool bool ; - .

, , ; , , , .

POD-, , fastcall .

, std::string - , , , RVO , , , - .

  • (rvalue)
  • .
  • , (STL, STD ..),
  • (lvalue), ,

, ++ 11. .

+4

, , , : .

, , ints bools, . , (.. - undefined , ). , , , , , . vector<int>*, vector<int>, ( ). , , . C ++, , Java #, , .

, , , , .. () , .

, : .

+3

.

( a.k.a.) , , . , , , , /self.

, , , - . .

/ (.. a factory), . , , , , factory? factory , ; factory , , factory , .

/ , , (), , ( / , ).

, , .

( ), . , , .

, , ; , , , , , , . , /.

+3

( )?   , .

, . , , ref.

.

, :

method1(list).method2(list)...
+2

, . , .

, , . , , ( struct). , .

, .

. , ( ) , . .

- , RAM , - . "return foo;" , foo , foo . , . , , .

. .

struct Big {
  int a[10000];
};

Big process(int n, int c)
{
  Big big;
  for (int i = 0; i < 10000; i++)
    big.a[i] = n + i;
  return big;
}

void process(int n, int c, Big& big)
{
  for (int i = 0; i < 10000; i++)
    big.a[i] = n + i;
}

XCode MacBook. return:

    xorl    %eax, %eax
    .align  4, 0x90
LBB0_1:                                 ## =>This Inner Loop Header: Depth=1
    leal    (%rsi,%rax), %ecx
    movl    %ecx, (%rdi,%rax,4)
    incq    %rax
    cmpl    $10000, %eax            ## imm = 0x2710
    jne     LBB0_1
## BB#2:
    movq    %rdi, %rax
    popq    %rbp
    ret

:

    xorl    %eax, %eax
    .align  4, 0x90
LBB1_1:                                 ## =>This Inner Loop Header: Depth=1
    leal    (%rdi,%rax), %ecx
    movl    %ecx, (%rdx,%rax,4)
    incq    %rax
    cmpl    $10000, %eax            ## imm = 0x2710
    jne     LBB1_1
## BB#2:
    popq    %rbp
    ret

, . , . -O1. , . gcc 4.2 .

, "". .

+1

:

  • ( );
  • , null.

, , , , .

, -, , , , . , , , .

, (const) , .

( : C.)

0

All Articles