Why are multi-variable return statements allowed?

NauticalMile

Simply stated, why does the following code compile?

#include<iostream>

int foo(){
    return 0,1;
}

int main(){
    int a,b = foo();
    std::cout << a << " " << b << std::endl;
    std::cout << foo();
    return 0;
}

I am using a 64-bit Window's machine, and compiling with Dev-C++ (using the MinGW GCC 4.5.2 32-bit compiler).

This code prints the following output:

2686824 1
1

I strongly suspect that the value contained in a is the usual garbage stored in uninitialized variables.

It is clear from this question that returning multiple values from functions is not common practice in industry, and it is definitely discouraged and penalized in programming courses taught at academic institutions.

So why does it work? As I've matured as a programmer, I've realized the incredible value of compiler errors, which are infinitely more intelligible than linker or run-time errors, and obviously way better than bugs.

To be clear, I'm more interested in why this has been allowed from a language design perspective, rather than the technical specifics of how the compiler does its thing (unless, in this case, implementation realities or technical consequences have made it difficult/impossible to detect/manage multiple return variables).

Are there some esoteric cases where multi-variable return statments were deemed useful?

Baum mit Augen

You still only return one value, which is 1.

return 0,1;

makes use of the comma operator, its result is its right hand side. With the right warning level, your compiler (gcc and clang at least) will warn because its left hand side has no effect.

If you actually want to return multiple values, you can return a std::tuple (or std::pair):

auto fun () {
    return std::make_tuple(0,1);
}

int main () {
    int a, b;
    std::tie(a,b) = fun();
    // Now a == 0, b == 1
}

Equivalent alternative as of C++17:

auto fun () {
    return std::make_tuple(0,1);
}

int main () {
    auto [a,b] = fun();
}

Live

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?

From Dev

Why are some useless statements partially allowed?

From Dev

Why are some useless statements partially allowed?

From Dev

Why Global variable redefinition is not allowed?

From Dev

Why is variable declaration not allowed here?

From Dev

Why is "LIMIT 0" even allowed in MySQL SELECT statements?

From Dev

Why isn't NEXT VALUE allowed in CREATE FUNCTION statements?

From Java

Why do my conditions (IF statements) return false?

From Dev

python - Why is it mandatory to use return statements in recursion?

From Dev

Why does the compiler not recognize my return statements?

From Dev

python - Why is it mandatory to use return statements in recursion?

From Dev

Javascript: Why using anonymous functions and return statements

From Java

Why type alias is allowed as name of variable?

From Dev

Why is jumping across variable definitions (with goto) allowed?

From Java

Why is a JavaScript reserved keyword allowed as a variable name?

From Dev

Why are special characters not allowed in variable names?

From Dev

Why is a JavaScript reserved keyword allowed as a variable name?

From Dev

Why is jumping across variable definitions (with goto) allowed?

From Dev

return multi variable to all blade files

From Dev

Why returning a member variable using "this" pointer is not allowed, but setting a member variable using "this" is allowed?

From Dev

Why compilation allowed when expected is return type mismatch?

From Dev

Why am I not allowed to return an IAsyncEnumerable in a method returning an IAsyncEnumerable

From Dev

Why am i allowed to pass an object of the bounding type of a generic type to a function, but not allowed to return it?

From Dev

Why is this allowed?

From Dev

why c doesn't check for return statements at compilation

From Dev

Why ReSharper breaks line in await and return statements differently?

From Dev

Why I cannot set break points on certain kind of return statements?

From Dev

Why is ;; allowed after a local variable declaration, but not after a field declaration?

From Dev

Why it is allowed to initialize static variable with non const here?

Related Related

  1. 1

    Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?

  2. 2

    Why are some useless statements partially allowed?

  3. 3

    Why are some useless statements partially allowed?

  4. 4

    Why Global variable redefinition is not allowed?

  5. 5

    Why is variable declaration not allowed here?

  6. 6

    Why is "LIMIT 0" even allowed in MySQL SELECT statements?

  7. 7

    Why isn't NEXT VALUE allowed in CREATE FUNCTION statements?

  8. 8

    Why do my conditions (IF statements) return false?

  9. 9

    python - Why is it mandatory to use return statements in recursion?

  10. 10

    Why does the compiler not recognize my return statements?

  11. 11

    python - Why is it mandatory to use return statements in recursion?

  12. 12

    Javascript: Why using anonymous functions and return statements

  13. 13

    Why type alias is allowed as name of variable?

  14. 14

    Why is jumping across variable definitions (with goto) allowed?

  15. 15

    Why is a JavaScript reserved keyword allowed as a variable name?

  16. 16

    Why are special characters not allowed in variable names?

  17. 17

    Why is a JavaScript reserved keyword allowed as a variable name?

  18. 18

    Why is jumping across variable definitions (with goto) allowed?

  19. 19

    return multi variable to all blade files

  20. 20

    Why returning a member variable using "this" pointer is not allowed, but setting a member variable using "this" is allowed?

  21. 21

    Why compilation allowed when expected is return type mismatch?

  22. 22

    Why am I not allowed to return an IAsyncEnumerable in a method returning an IAsyncEnumerable

  23. 23

    Why am i allowed to pass an object of the bounding type of a generic type to a function, but not allowed to return it?

  24. 24

    Why is this allowed?

  25. 25

    why c doesn't check for return statements at compilation

  26. 26

    Why ReSharper breaks line in await and return statements differently?

  27. 27

    Why I cannot set break points on certain kind of return statements?

  28. 28

    Why is ;; allowed after a local variable declaration, but not after a field declaration?

  29. 29

    Why it is allowed to initialize static variable with non const here?

HotTag

Archive