0
string choices()
{
    string ch1 = "1",ch2 = "2",ch3 = "3",ch4 = "4",ch5 = "5",ch6 = "6",ch7 = "7",ch8 = "8",ch9 = "9";
    return ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9;
}

void display(string a,string b,string c,string d,string e,string f,string g,string h,string i)
{
    cout << a << b << c << d << e << f << g << h << i;
    return;
}

int main()
{
    string a,b,c,d,e,f,g,h,i;
    a,b,c,d,e,f,g,h,i = choices(); 
    display(a,b,c,d,e,f,g,h,i);  //display data

    return 0;
}

How to return multiples variables from function ? Do I need to pass by reference?

isme_
  • 13
  • 3
  • Why do you need to return multiple values from a function? What is the actual and underlying problem that is supposed to solve? And if it's just plain curiosity (which is okay) then please state that. – Some programmer dude Oct 15 '20 at 10:12
  • 3
    As for your problem, are the variables you want to return closely related? Then use a structure or a class. Otherwise you can use [`std::tuple`](https://en.cppreference.com/w/cpp/utility/tuple). Or perhaps rethink your design. – Some programmer dude Oct 15 '20 at 10:13
  • 2
    Passing by reference maybe the thing you're looking for. But it's quite unclear what you're asking – Jabberwocky Oct 15 '20 at 10:13
  • yeah,I just curious whether we can return multiples variables or not – isme_ Oct 15 '20 at 10:13
  • 1
    Off-topic, but did you really need *nine* variables to illustrate your question? Two would be enough, no? – molbdnilo Oct 15 '20 at 10:24
  • I didnt notice that.btw thank you. – isme_ Oct 15 '20 at 10:27

1 Answers1

3

Use tuple and structured bindings:

auto choices() {
  std::string ch1 = "1",ch2 = "2",ch3 = "3",ch4 = "4",ch5 = "5",ch6 = "6",ch7 = "7",ch8 = "8",ch9 = "9";
  return std::tuple {ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9};
}
auto [a,b,c,d,e,f,g,h,i] = choices();
Equod
  • 556
  • 3
  • 13