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?
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.
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.
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.
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.
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]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With