Function to return two variables? BAH GAWD MAN!


so I want to try to keep alot of my work in functions, like some C++ sage told me to do, but I keep running into tiny problems, observe:



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
////  THIS IS JUST A SAMPLE I COOKED UP
///   PLEASE DO NOT CRITIQUE THIS AS IF IT 
///   WERE REALLY GOING TO BE USED IN SOMETHING
///   THE REAL CODE IS HIGHLY ULTRA TOP SECRET

#include <iostream>
#include <Windows.h>


using namespace std;

void user_input();

int main()
{
	 user_input();

}


void user_input()
{
	int randNum, randNum2;

	cout << "Enter random number, you disgusting piece of garbage.\n>";
	cin >> randNum;

	cout << "Hope you choke on a salad." 
		<< "Enter another random number, clown face.\n>" ;
	cin >> randNum2;

		return;


}


I want to return the two variables, randNum and randNum2 to the main block so that I can use them for other things. Wat do?

Your sample is actually good, concise and to the point.

Functions in C++ can only return one value. That is not usually considered as a big inconvenience. There are many ways to get around it, depending on your level of knowledge and other considerations.

Here are some possibilities:

1. make two functions, each one returning a single value

2. return a structure containing multiple values

3. return std::pair or std::tuple

4. use references to return values through function parameters.
Last edited on
Also: That C++ sage gave you good advice, IMO.
There are two methods.
1) Return a struct which contains the two variables
1
2
3
4
5
6
7
8
9
10
11
12
struct twoval
{  int r1;
    int r2;
};

twoval user_input()
{  twoval tv;
...
  tv.r1 = randNum;
  tv.r2 = randNum2;
  return tv;
}


2) Pass arguments by reference
1
2
3
4
5
6
void user_input (int & r1, int &r1)
{
...
  r1 = randNum;
  r2 = RandNum2;
}

Last edited on
edit; I used the STRUCT method

How do I return the structure?



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

struct input_ {

int randNum;
int randNum2;
};

int main()
{

// how to return it?

//  user_input();  // does not work.
// input_ user_input();  // builds successfully but does not fire the function, has error:

/* Warning	1	
warning C4930: 'input_ user_input(void)': prototyped function not called (was a variable definition intended?)	
e:\33visualstudio\array_test\array_test\array_source.cpp	33
*/


}

void user_input() {

...


}
Last edited on
I gave you the code to do that in the first snippet of my previous post.

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
#include <iostream>
using namespace std;

struct input_ 
{   int randNum;
    int randNum2;
};

input_ user_input();

int main()
{   input_  inp;

	inp = user_input();

}

input_ user_input()
{   input_  inp;

	cout << "Enter random number, you disgusting piece of garbage.\n>";
	cin >> inp.randNum;

	cout << "Hope you choke on a salad." 
		<< "Enter another random number, clown face.\n>" ;
	cin >> inp.randNum2;
	return inp;
}

Lets keep it a bit more tidy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct ReturnData {
	int randNum;
	int randNum2;
};

RetunData user_input();

int main ...

RetunData user_input()
{
	RetunData rd;
	...
	return rd;
}
Ok got it,

thanks guys; I had spelled the function wrong which is why it didn't work...
Topic archived. No new replies allowed.