Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can I get two values from a return type function?

Please give an example.
Posted
Updated 29-Mar-11 7:32am
v3

I think the easiest choice for you is to use SA's answer:
C++
//define a result structure
struct RESULT
{
    //add as many members as you want here...
    int result1;
    float result2;
};

//this function returns a struct
RESULT function()
{
    //declare the value you will return
    RESULT returnValue;
    //fill the fields
    returnValue.result1 = 5;
    returnValue.result2 = 12.3f;
    //return the value
    return returnValue;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Mar-11 12:27pm    
Oh, here is the example! My 5.
Teamwork :-)
--SA
Olivier Levrey 29-Mar-11 12:30pm    
he he thanks ;)
Sergey Alexandrovich Kryukov 29-Mar-11 12:40pm    
Well, to make it really usable, add a constructor to RESULT, so your last 3 lines in "function" will be replaced with just one return line.
Thanks again; I referenced your post in mine.
--SA
Dalek Dave 29-Mar-11 13:33pm    
Good Call
In order to return two values you can do several different things:

1. Return an object. Define a class and return an instance of that class.
2. Return a structure.
3. Return an array.
4. Use reference parameters (altough this is not returning in the purest meaning of it).

The answers you've received before are nice.

HTH!
 
Share this answer
 
Comments
Olivier Levrey 29-Mar-11 12:23pm    
Everything is correct, but OP asked for examples. My 5 anyway.
Sergey Alexandrovich Kryukov 29-Mar-11 12:28pm    
Mine too. Joan, you need to reference and up-vote Olivier's Answer for making that example.
--SA
Most API functions do it in this way.
* return with error value - to check whats wrong
* parameter with struct size for version checking
* parameter with flags (bitfield) to mark the valid members

typedef enum
{
  RESULTOK,
  INVALIDPARAM,
  BUFFERTOOSMALL,
  // and so on

} MYERRORS;

typedef struct
{
  unsigned int  cbStruct; // ==sizeof(VARIABLE_RESULT_LIST)
  enum
  {
    VALUE1 = 1<<0,
    VALUE2 = 1<<1,
    VALUE3 = 1<<2,
    RESULT = 1<<2,
  };
  unsigned int  fValdMask;
  
  unsigned int  uiValue1;
  short          sValue2;
  TCHAR*        lpValue3;
  unsigned int  ccValue3;

  unsigned int  uiAnyResult;

} VARIABLE_RESULT_LIST;

MYERRORS MyVariableFunction(VARIABLE_RESULT_LIST* pVar)
{
  if(!pVar)
    return INVALIDPARAM;
  if(sizeof(VARIABLE_RESULT_LIST)>pVar->cbStruct)
    return BUFFERTOOSMALL;

  if(VARIABLE_RESULT_LIST::VALUE1 & pVar->fValdMask)
  {
    pVar->uiAnyResult = 42 + pVar->uiValue1;
    pVar->fValdMask |= VARIABLE_RESULT_LIST::RESULT;
  }
  else
  {
    pVar->fValdMask &= ~VARIABLE_RESULT_LIST::RESULT;
  }
  
  if(VARIABLE_RESULT_LIST::VALUE3 & pVar->fValdMask)
  {
    if(pVar->lpValue3 && (_tcslen(__T("Hello world"))<pVar->ccValue3))
      _tcscpy_s(pVar->lpValue3,pVar->ccValue3,__T("Hello world"));
    else // buffer too small
      pVar->fValdMask &= ~VARIABLE_RESULT_LIST::VALUE3;
  }

  // ignored in this version
  pVar->fValdMask &= ~VARIABLE_RESULT_LIST::VALUE2;

  return RESULTOK;
}

void main()
{
  VARIABLE_RESULT_LIST  var = { sizeof(VARIABLE_RESULT_LIST), };
  TCHAR                  str[256];
  
  var.fValdMask = 0;

  var.uiValue1  = 23;
    var.fValdMask |= VARIABLE_RESULT_LIST::VALUE1;

  var.lpValue3  = str;
  var.ccValue3  = sizeof(str)/sizeof(str[0]);
    var.fValdMask |= VARIABLE_RESULT_LIST::VALUE3;
    
  if(RESULTOK==MyVariableFunction(&var))
  {
    if(VARIABLE_RESULT_LIST::VALUE1 & var.fValdMask)
      _tprintf(__T("uiValue1: %i\r\n"),var.uiValue1);
    if(VARIABLE_RESULT_LIST::VALUE2 & var.fValdMask)
      _tprintf(__T("sValue2: %i\r\n"),(int)var.sValue2);
    if(VARIABLE_RESULT_LIST::VALUE3 & var.fValdMask)
      _tprintf(__T("lpValue3: %s\r\n"),var.lpValue3);
  }
}

Regards.
 
Share this answer
 
Use std::tuple along with std::tie like this to return three values:

#include <tuple>
std::tuple<int, int, int> func()
{
    return std::make_tuple(1, 2, 3);
}
int main()
{
    int r1, r2, r3;
    std::tie(r1, r2, r3) = func();
}


If you really need just two values, you can also use std::pair instead.
 
Share this answer
 
v2
Look at the C++ keywords class and struct. "Two values" would be two fields in those types which you have to define.

For the example, look at the Answer by Olivier.

—SA
 
Share this answer
 
v2
Comments
Olivier Levrey 29-Mar-11 12:25pm    
My 5. Even though OP asked for an example, this should be enough to help.
Sergey Alexandrovich Kryukov 29-Mar-11 12:26pm    
Example?.. Hm...
Thank you, Olivier.
--SA
Return an array this will solve ur problem
Ex:-


Public IList GetList()
{
IList lst =new ArrayList();
lst.Add("a");
lst.Add("b");

return lst;

}



Hope this will solve ur problem :)
 
Share this answer
 
Comments
Olivier Levrey 29-Mar-11 12:23pm    
A word about deleting the return value would be nice. Even though this example works, it could lead to memory leaks if the user is not carefull...
Nemanja Trifunovic 29-Mar-11 13:14pm    
The question is tagged with C, C++. Your example is in neither of these languages.
UL UL ALBAB 29-Mar-11 15:36pm    
:D

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