Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple return values in function

Tags:

c++

c

I've faced the next one sample:

#include <stdio.h>

// test multiple return                                                                                                                                                             
int foo()
{
    return 1,2,3,4,5,6;
}

// main entry point                                                                                                                                                                 
int main(int argc, char* argv[])
{
    printf("foo returns: %d\n", foo());
    return 0;
}

compile it, then run:

gcc main.cpp -o main
./main

The results are confusing me:

foo returns: 6

The question is: why there is no compile time error?

like image 947
egor7 Avatar asked Jan 26 '12 09:01

egor7


People also ask

Can a function have multiple values?

In mathematics, a multivalued function, also called multifunction, many-valued function, set-valued function, is similar to a function, but may associate several values to each input.

How do I store two values returned from a function?

Return multiple values using commas In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax.

Can I return 2 values from a function in Python?

You can return multiple values from a function in Python. To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week. Data structures in Python are used to store collections of data, which can be returned from functions.


2 Answers

In this context:

return 1,2,3,4,5,6;

is actually the comma operator. It evaluates everything between the commas in order (left-to-right), and returns the last one.

That's why it returns and prints 6. So yes, it's valid code. That's why there's no compiler error. (Although the 1,2,3,4,5 part doesn't do anything in this case.)

In C and C++, you can't return multiple values. You'd have to use a struct or a class to do that.

like image 101
Mysticial Avatar answered Oct 14 '22 14:10

Mysticial


Because you are using the comma operator: the expression a,b where a and b are arbitrary (usually side-effecting) sub-expressions mean: evaluate the left-hand side a and discard its result (so a is only evaluated for side-effects), then evaluate b and give it as result.

You cannot return several things from a C function. You should return e.g. an aggregate (usually a struct) or a dynamically heap-allocated pointer.

As to the question, why the compiler don't say anything? Because you didn't ask it. You really should compile with gcc -Wall (for C code) or g++ -Wall (for C++ code), and then you get warnings:

 egor7.c: In function ‘foo’:
 egor7.c:6:13: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:15: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:17: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:19: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:21: warning: left-hand operand of comma expression has no effect [-Wunused-value]
like image 40
Basile Starynkevitch Avatar answered Oct 14 '22 15:10

Basile Starynkevitch