C++ return value from function
-
Hi there, i was testing this the other day, and it compiles fine.
int Test(int a, int b, int c) { return a,b,c; }
doing this;printf("\n %d", Test(10,20,30));
will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008) -
Hi there, i was testing this the other day, and it compiles fine.
int Test(int a, int b, int c) { return a,b,c; }
doing this;printf("\n %d", Test(10,20,30));
will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)The way the comma operator works is that it returns the last value. For instance:
int a = 1, 2, 3;
will result in
a
being 3. So yourreturn a,b,c;
is really equivalent to:
return c;
-
The way the comma operator works is that it returns the last value. For instance:
int a = 1, 2, 3;
will result in
a
being 3. So yourreturn a,b,c;
is really equivalent to:
return c;
-
for example it's used in the
for
statement.for ( i = 0, j = 0;; i<100; i++, j++)
{
// ...
}or in variable declarations:
int i, j;
and maybe many other places.
This signature was proudly tested on animals.
-
It can be overloaded. See Boost Assign[^] library for example:
vector<int> v;
v += 1,2,3,4,5,6,7,8,9; -
Hi there, i was testing this the other day, and it compiles fine.
int Test(int a, int b, int c) { return a,b,c; }
doing this;printf("\n %d", Test(10,20,30));
will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)uus831 wrote:
Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
The comma operator evaluates bot operands, and evaluates to the result of the second one. So the first operand is evaluated only for its side effects. One common use is:
while ( (c=getnextthingie(), c!=EOF) ) { }
here, the first operand has the side effect of calling getnextthingie and assigning the result to c. c!=EOF is evaluated as loop condition.Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server -
Hi there, i was testing this the other day, and it compiles fine.
int Test(int a, int b, int c) { return a,b,c; }
doing this;printf("\n %d", Test(10,20,30));
will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)Er, yeah, if you COULD declare your function as: int, int, int Test(int a, int b, int c); You might get 12 bytes on the stack for your return values, but C isnt made this way! If you realy want to wedge more values into the 32 bits you are given you could cast say a short and two chars into an int. return (x << 16 + y << 8 + z); for example, and then pull it apart in the calling code.
Morality is indistinguishable from social proscription