Having 2 separate variables get values from one function

I have a question,
Is it possible to have 2 variables be set to 2 variables that are returned in a function, Its hard to explain.
So I have 2 variables, that I want to set there values from 1 function.
For example
string name;
int age;

The Function is something like this
string information(string name, int age) {
name = "name";
age = rand()%50+1;
return name;
return age;
}

Could you do something like
name, age = information(name, age);
Having 2 separate variables get values from one function
Last edited on
If the variables already exist and you're *replacing* their prior values with the results of the function, use std::tie:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include <tuple>
std::pair<std::string, int> information()
{
  return {"name", 7};
}

int main()
{
   std::string name = "old value";
   int age = -1;
   std::tie(name, age) = information();
   std::cout << name << ':' << age << '\n';
}

live demo https://wandbox.org/permlink/uIT0uAlscB4vNwgH

If you're creating new variables, this is only directly supported as of C++17:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>
#include <tuple>
std::pair<std::string, int> information()
{
  return {"name", 7};
}

int main()
{
   auto [name, age] = information();
   std::cout << name << ':' << age << '\n';
}

live demo: https://wandbox.org/permlink/F8Ifsc9aQimY2TXs
Wow! Thanks very much.
Yeah I am *replacing* their prior values with the results of the function.
you could also pass both variables by reference, and replace them from within the function:

1
2
3
4
void information(string& name, int& age) {
  name = "name";
  age = rand()%50+1;
}

and you can use typedef (simple array, etc) or struct (mixed types) or a pointer/vector/many things to create a custom return type and pull it out of that.

struct returnme
{
sometype a;
othertype b;
};

returnme myfunc(int paramater)
{

...
return result;
}

tmp = myfunc(param);
oldval1 = tmp.a;
oldval2 = tmp.b;

With the modern stuff the only reason I see to do this is if the 2 returned variables are not the same type? Or is there a clever way to do that too?
Last edited on
When I want to return two or more values from a function I usually create a class to hold them and return an anonymous variable of this custom type.

So, the multiple return class header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
class CMultipleReturn{
public://Constructor and destructor.
     CMultipleReturn();
     CMultipleReturn(const int iReturnOne, const int iReturnTwo);
     CMultipleReturn(const CMultipleReturn &cSource);
     CMultipleReturn(CMultipleReturn &&cSource);
     ~CMultipleReturn();
public://Public accessor member functions.
     const int GetReturnOne() const {return m_iReturnOne;);
     const int GetReturnTwo() const {return m_iReturnTwo;);
private:
     int m_iReturnOne;
     int m_iReturnTwo;
};


Its implementation file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <CMultipleReturn.h>

CMultipleReturn::CMultipleReturn() :
	m_iReturnOne{ 0 },
	m_iReturnTwo{ 0 }
{
}

CMultipleReturn::CMultipleReturn(const int iReturnOne, const int iReturnTwo) :
	m_iReturnOne{ iReturnOne },
	m_iReturnTwo{ iReturnTwo }
{
}

CMultipleReturn::CMultipleReturn(const CMultipleReturn &cSource) :
	m_iReturnOne{ cSource.m_iReturnOne },
	m_iReturnTwo{ cSource,m_iReturnTwo }
{
}

CMultipleReturn::CMultipleReturn(CMultipleReturn &&cSource) :
	m_iReturnOne{ cSource.m_iReturnOne },
	m_iReturnTwo{ cSource,m_iReturnTwo }
{
	cSource.m_iReturnOne = 0;
	cSource.m_iReturnTwo = 0;
}

CMultipleReturn::operator=(const CMultipleReturn &cSource)
{
	m_iReturnOne = cSource.m_iReturnOne;
	m_iReturnTwo = cSource.m_iReturnTwo;
}

CMultipleReturn::operator=(CMultipleReturn &&cSource)
{
	m_iReturnOne = cSource.m_iReturnOne;
	m_iReturnTwo = cSource.m_iReturnTwo;

	cSource.m_iReturnOne = 0;
	cSource.m_iReturnTwo = 0;
}


When used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "CMultipleReturn.h"

const CMultipleReturn FunctionThatDoesSomething();

int main()
{
     CMultipleReturn cReturn;

     cReturn=FuctionThatDoesSomething();

     printf("The first value is %i\nThe second value is %i",
          cRetuen.GetReturnOne(),
          cReturn.GetReturnTwo());
}

const CMultipleReturn FuctionThatDoesSomething()
{
     int iFirstValue{0};
     int iSecondValue{0};

     ...//Code follows which put something useful in to these two integers.

     return CMultipleReturn{iFirstValue, iSecondValue};
}


It would be relatively easy--I think!--to use templates and make the same class usable with any return data type you wanted. As I mentioned in another thread I am still finding my feet again in the newest versions of the language, so apologies for any obvious errors. That is just a class I was able to literally copy and paste from the programme I currently have open in front of me and am pottering about with right now.
Last edited on
Topic archived. No new replies allowed.