23
int DFS(a, b,c,d)
{
    first=a+b;
    second=c+d;
    return(first,second);
}

solution, cost_limit = DFS(a, b,c,d);

can I do something like this ? and how?

1
  • It's very unclear what you're asking here. I would suggest you explain what you mean by assign, and what you mean by a variable. Commented Jan 12, 2012 at 10:55

5 Answers 5

33

In C++11 you can use the tuple types and tie for that.

#include <tuple>

std::tuple<int, int> DFS (int a, int b, int c, int d)
{
    return std::make_tuple(a + b, c + d);
}

...

int solution, cost_limit;
std::tie(solution, cost_limit) = DFS(a, b, c, d);
Sign up to request clarification or add additional context in comments.

5 Comments

If C++11 is not an option, the Boost Tuple library provides these features.
If C++11 is not an option, you still can use C++03's std::pair<int, int>
@Antionio: but there's no tie in C++03, so it comes out looking pretty ugly: std::pair<int, int> result = DFS(a,b,c,d); solution = result.first; cost_limit = result.second;.
You could use TR1, I think that one also features the tuple library (with tie)
in C++17, use structured declarations with pair
17

With C++17, you can unpack a pair or a tuple

auto[i, j] = pair<int, int>{1, 2};
cout << i << j << endl; //prints 12
auto[l, m, n] = tuple<int, int, int>{1, 2, 3};
cout << l << m << n << endl; //prints 123

2 Comments

And you don't need the <int, int> anymore BTW: stackoverflow.com/questions/9270563/…
This is bundled with declaration. The question is how to assign to existing variable.
1

You can do this two ways:

  1. Create a struct with two values and return it:

    struct result
    {
        int first;
        int second;
    };
    
    struct result DFS(a, b, c, d)
    {            
        // code
    }
    
  2. Have out parameters:

    void DFS(a, b, c, d, int& first, int& second)
    {
        // assigning first and second will be visible outside
    }
    

    call with:

    DFS(a, b, c, d, first, second);
    

2 Comments

This is clearly not what the OP is asking for. He's asking for returning two separate values into two separate variables.
This is not what he asked for. He wants the pythonic way, meaning solution = first; cost_limit = second;
0

One thing you should be know is that if a,b,c,d are not base types, but instances of a class you defined, let's say Foo, and you overload the = operator of the class, you must ensure the fact that the operator will return a reference to the object which is assigned, or else you will not be able to chain assignments ( solution = cost_limit = DFS(..) will only assign to cost_limit). The = operator should look like this:

Foo& Foo::operator =(const Foo& other)
    {
       //do stuff
       return other;
    }

Comments

0

If C++11 is not possible, it's possible to use references.

By passing a reference to variables in parameters.

int DFS(int a, int b, int c, int d, int &cost_limit)
{
    cost_limit = c + d;
    return a + b;
}

int solution, cost_limit;

solution = DFS(a, b, c, d, cost_limit);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.