Returning Multiple Values from a Function

Hello, I need to return two separate values from the following function...within the function is a loop that compares variables and is supposed to return the numeric value of the matching variables. If there are 2 "sets" of matching variables, the function needs to be able to return as many as two values...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using namespace std;
int PairReturn(int n1, int n2, int n3, int n4, int n5)
{
	int numbs[5]= {n1, n2, n3, n4, n5}, n = 5;
		
	int j=1;
	for (int i = 0; (i < n - 1); i++)
        {			
			
			for (j = i + 1; (j < n); j++)
			{
				if (numbs[i] == numbs[j]) return numbs[j];
				
			}			
				
        }	
	return (0);	
}


This function stops after finding two matching variables (a pair)...It returns the numeric value of the pair. I would like to have this function return as many as 2 numeric values if the function finds that there are two pairs. Any ideas?
Last edited on
Declare an array before your function and then add any values you wish to into that. If you do this you won't need to return any values.
Last edited on
How do I add the values from this function into the array? The values are determined by the function...I've heard of refering to a variable but I'm not sure how to apply it into this situation...
That's an unusual requirement. I might have suggested that you use the std::pair object:
 
std::pair<int, int> PairReturn(int n1, int n2, int n3, int n4, int n5);

However, what if you only get one match?

I would be tempted to pass in a std::vector by reference and store your one or two results in that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <iostream>

void GetReturn(int n1, int n2, int n3, int n4, int n5, std::vector<int>& matches)
{
	int numbs[5]= {n1, n2, n3, n4, n5}, n = 5;
		
	int j=1;
	for (int i = 0; (i < n - 1); i++)
	{			
		for (j = i + 1; (j < n); j++)
		{
			if (numbs[i] == numbs[j]) matches.push_back(numbs[i]);
		}			
	}	
}

That method may also return more than 2...
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	std::vector<int> matches;

	GetReturn(1,3,3,5,7, matches);

	for(size_t i = 0; i < matches.size(); ++i)
	{
		std::cout << "match: " << i << " = " << matches[i] << '\n';
	}
}

You might also consider passing in your numbers in a vector rather than a long list of parameteres:
1
2
3
4
void GetReturn(const std::vector<int>& numbers, std::vector<int>& matches)
{
	...
}
To be honest i would do it with a vector because of the member functions you get with them (push_back() in particular). Vectors are very similar to arrays if you haven't used them before

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 <vector>

using namespace std;

std::vector<int> pairvalues(100);

int PairReturn(int n1, int n2, int n3, int n4, int n5)
{
	int numbs[5]= {n1, n2, n3, n4, n5}, n = 5;
		
	int j=1;
	for (int i = 0; (i < n - 1); i++)
        {			
			
			for (j = i + 1; (j < n); j++)
			{
				if (numbs[i] == numbs[j]) pairvalues.push_back(numbs[j]);//this will add the value onto the end of your vector
				
			}			
				
        }	
	return (0);	
}
Last edited on
Do you know how to use a struct or a class? That would allow you to group your returned data into one object so that it could all be returned at once. http://www.cplusplus.com/doc/tutorial/classes/

Passing the values in by reference really wouldn't help you in this case since they are just regular ints.

EDIT: @ chr15chr15: Declaring the size of the vector needlessly limits it, you should not make that into a habit.
Last edited on
Point noted computergeek. However can you say it needlessly limits it. Surely if there is a limit which your vector won't surpass you are just freeing up memory?

edit: i admit in this circumstance i shouldnt have wrote 100!:)
Last edited on
You're micromanaging is what you are doing. It won't hurt the program in this case, but stuff like that can be difficult to unlearn, as well as give a reader the wrong impression about vectors. The key strength of the STL containers is that they manage their own dynamic memory allocation, so there is very little need to preset a size like that.
returning a struct sounds like the best solution I will work it out, thanks everyone
closed account (3hM2Nwbp)
Sounds like this could be a job for a varardic template to me. (C++0x only, of course)

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
#include<list>

template<int... Integers>
std::list<int> PairReturn(void)
{
  int n = sizeof...(Integers);
  int numbs[sizeof...(Integers)] = { Integers... };
  std::list<int> pairs;
  int j = 1;
  for (int i = 0; (i < n - 1); i++)
  {			
    for (j = i + 1; (j < n); j++)
    {
      if (numbs[i] == numbs[j])
      {
        pairs.push_back(numbs[j]);
      }
    }			
  }
  return pairs;
};


#include <iostream>

int main()
{
  std::list<int> pairs = PairReturn<0, 1, 2, 2, 3, 4, 5>();
  std::list<int> morePairs = PairReturn<0, 0, 2, 4, 2, 4, 2, 4, 5, 6, 7, 8, 4, 4, 6, 19, 33, 1773>();
  for(std::list<int>::const_iterator iter = pairs.begin(); iter != pairs.end(); ++iter)
  {
    std::cout << "Pair: " << *iter << std::endl;
  }
  return 0;
}


Regards.
Last edited on
Then you lose all dynamic abilities...
closed account (3hM2Nwbp)
Hence the could.
Topic archived. No new replies allowed.