C++ -- return x,y; What is the point?
I have been programming in C and C++ for a few years and now I'm just now taking a college course in it and our book had a function like this for an example:
int foo(){
int x=0;
int y=20;
return x,y; //y is always returned
}
I have never seen such syntax. In fact, I have never seen the ,
operator used outside of parameter lists. If y
is always returned though, then what is the point? Is there a case where a return statement would need to be created like this?
(Also, I tagged C as well because it applies to both, though my book specifically is C++)
According to the C FAQ:
Precisely stated, the meaning of the comma operator in the general expression
e1 , e2
is "evaluate the subexpression e1, then evaluate e2; the value of the expression is the value of e2." Therefore, e1 had better involve an assignment or an increment ++ or decrement -- or function call or some other kind of side effect, because otherwise it would calculate a value which would be discarded.
So I agree with you, there is no point other than to illustrate that this is valid syntax, if that.
If you wanted to return both values in C or C++ you could create a struct
containing x
and y
members, and return the struct instead:
struct point {int x; int y;};
You can then define a type and helper function to allow you to easily return both values within the struct
:
typedef struct point Point;
Point point(int xx, int yy){
Point p;
p.x = xx;
p.y = yy;
return p;
}
And then change your original code to use the helper function:
Point foo(){
int x=0;
int y=20;
return point(x,y); // x and y are both returned
}
And finally, you can try it out:
Point p = foo();
printf("%d, %d\n", p.x, p.y);
This example compiles in both C and C++. Although, as Mark suggests below, in C++ you can define a constructor for the point
structure which affords a more elegant solution.
On a side note, the ability to return multiple values directly is wonderful in languages such as Python that support it:
def foo():
x = 0
y = 20
return x,y # Returns a tuple containing both x and y
>>> foo()
(0, 20)
The comma in parameter lists is just there to separate the parameters, and is not the same as the comma operator. The comma operator, as in your example, evaluates both x and y, and then throws away x.
In this case, I would guess that it is a mistake by someone who tries to return two values, and didn't know how to do it.
The comma operator is primarily used in for
statements like so:
for( int i=0, j=10; i<10; i++, j++ )
{
a[i] = b[j];
}
The first comma is not a comma operator, it's part of the declaration syntax. The second is a comma operator.
This doesn't really answer the original question at all but might be of interest to some people, but if you wanted to it to return both in C++ you'd need to write it like this (and would need a c++0x compiler)
tuple<int, int> foo()
{
int x = 0;
int y = 20;
return make_tuple(x, y);
}
The access it like this -
tuple<int, int> data = foo();
int a = get<0>(data);
int b = get<1>(data);
struct Point {
int x, y;
Point(int x_) : x(x_), y(0) {}
Point(const Point& p) : x(p.x), y(p.y) {}
Point operator, (int y_) const { Point p=*this; p.y = y_; return p; }
};
Point get_the_point () {
int x = 0;
int y = 20;
return (Point)x, y;
}
:p