return from function

H

Huub

Hi,

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even
possible what I want: returning 2 integers?

Thanks.
 
M

Marc

Huub said:
I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even
possible what I want: returning 2 integers?

You can only return one thing from a function. But nothing prevents
that thing from being a std::pair of integers. The other usual
solution is to pass integers as arguments by reference to the
function.
 
J

Joshua Maurice

Hi,

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even
possible what I want: returning 2 integers?

Thanks.

"return x,y;" is parsed as a return statement with an expression
"x,y". x and y are names of integer variables. The expression "x,y" is
two expressions "joined" by the comma operator. The comma operator
evaluates the first argument, has a sequence point, then the second
argument. The result of a comma operator is the evaluation of the
second expression.

Note that commas in function calls are not comma operators, and commas
in other contexts are not necessarily comma operators either.
Consider:
f(x, y); //not a comma operator
f((x, y)); //is a comma operator
x, y; //is a comma operator
return x, y; //is a comma operator
int x, y; //not a comma operator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top