Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
There is any way we can return two variables in a function. In the following function I would like to return result and counter. Can anyone help me please?


C#
int repetition(int array3[])
{
    int copy[100];

    int result = 0;
    int number;
    for (int i = 0; i<100; i++)
    {

        copy[i]=array3[i];
    }

    for (int i = 0; i < 100; i++)
    {
        int counter = 0;
        for (int j = 0; j < 100 ; j++)
        {
            if (array3[i] == copy[j])
            {
                counter++;
            }
            if (counter > result )
            {
                result = counter;
                number = array3[i];
            }
        }

    }

return result, number;

}
Posted

XML
std::pair<int, int> repetition(int array3[])
{
   // :
   return std::make_pair(result, number);
}

std::pair<int, int> answer = repetition(arr);
std::cout << answer.first << ", " << answer.second << std::endl;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-May-14 0:33am    
This is just one of the ways. Voted 4.
Please see Solution 2 for more comprehensive answer.
—SA
Maciej Los 11-May-14 10:41am    
+5
There are some options. Solution 1 is one of them. A similar solution is to return an array with two elements. These two solutions are not very clean, because they are not sound in terms of semantics of the two values returned. In some (more or less rare cases) this is what's needed.

If the values carry distinct semantics, you should better use clearer approach: create a structure or a class with two instance member of required types and return the instance of this type.

Also, let's consider the way beyond return. Alternatively, you can pass a parameter by reference. If you change the parameter value, it will be modified on output. Please see: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference[^].

And a final, side note: many developers do a great code design mistake: returning some status or error code from functions. This approach is badly obsolete and bad. With C++ and most modern languages, you shouldn't return anything like that but should throw an exception.

—SA
 
Share this answer
 
v2
Comments
chandanadhikari 11-May-14 4:52am    
5 for the link and the explanation and all!
Sergey Alexandrovich Kryukov 11-May-14 10:14am    
Thank you very much.
—SA
Maciej Los 11-May-14 10:39am    
Valueable answer. I have no idea why it has been downvoted.
Sergey Alexandrovich Kryukov 11-May-14 14:48pm    
Thank you, Maciej.
Here we go again. Someone with highest score (-16 in down-votes) down-votes several answers in a row, during few days. Such attacks happen from time to time. Looks like "emotional voting" to me...
—SA
Maciej Los 11-May-14 15:43pm    
Sometimes it happens to me too... ;(
I do not understand such behaviour and do not accept it. In my opinion, it's a matter of honor. I'm not afraid to down-vote someones answer. But i'm not doing it anonymous.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900