call by reference

Hi All,

Can anyone tell me the difference of these 2 types of function:

case1:
int& abc(int a, int b, int c, int& result){

result = a + b + c;
return result;
}

case2:
void abc(int a, int b, int c, int& result){

result = a + b + c;
return 0;
}
first one returns a reference to int (it returns passed in reference to result)
second one is invalid and should not compile
Thx MiiNiPaa.

Sorry I mistakenly added "return 0". Taking out this line, other things compiles (I tested). My main question is about the way of transfer variables. If result is a vector, I should use reference for calling. case1 and case2 both work. I am not clear about the difference between these 2. And which one is better?

Many thanks.
only difference is that first returns reference to result back allowing for chaining:
1
2
3
int result  = 0;
int result2 = 0;
std::cout << abc(1, 2, abc(4, 5, 6, result), result2);

With second case you should write:
1
2
3
4
5
int result = 0;
int result2 = 0;
abc(4, 5, 6, result2)
abc(1, 2, result2, result);
std::cout << result;
Thanks for the reply.
Topic archived. No new replies allowed.