# Return Multiple Values in a Function

Page 1 of 1

## 6 Replies - 140386 Views - Last Post: 25 June 2010 - 03:49 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26506&amp;s=065ec40506986a1eeea38dd1580b9477&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 freshman_prog

Reputation: 1
• Posts: 23
• Joined: 23-March 07

# Return Multiple Values in a Function

Posted 13 April 2007 - 01:41 PM

i m trying to return more than one value its not work.
Is This A Good Question/Topic? 1

## Replies To: Return Multiple Values in a Function

### #2 girasquid

• Barbarbar

Reputation: 108
• Posts: 1,825
• Joined: 03-October 06

## Re: Return Multiple Values in a Function

Posted 13 April 2007 - 01:57 PM

That's because you're only allowed to return one value, unless you return an array of values.

### #38 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Return Multiple Values in a Function

Posted 13 April 2007 - 02:13 PM

POPULAR

Well C/C++ does not really let you return more than one value... there are ways arround this.

#1 Use a structure:

```typedef struct {
int x;
int y;
} Coodinate;

Coordinate ShiftX(Coordinate a, int offset)
{
Coordinate ret;
ret.x=a.x+offset;
ret.y=a.y;
return ret;
}
```

Another way to get arround this is to pass values by referance
```void foo(int &fe, int &fi, int fo)
{
fe += fo;
fi += fe;
return;
}

...main()...
int A=0, B=1, C=2;
foo(A,B,C);
cout << A <<'\t'; //A=2
cout << B <<'\t'; //B=3
cout << C <<'\n'; //C=2

```
This could also be done with pointers.

Very often function have a special "results" structure and they require that the calling function send them a pointer to an instance of the structure (or just a buffer large enough to hold the structure) (This is very common when interfaceing with operating systems and BIOS).

```typedef struct {
char InvestorID[30];
double InitalInvestment;
double Rate;
date StartDate;
} Investment;

void GetUserInput(Investment *User, int offset);

...main...
Investment Users[100];
for (int i=0; i<100;++i)
{
GetUserInput(Users, i);
}
```

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Return Multiple Values in a Function

Posted 13 April 2007 - 02:39 PM

A simple manner in which to return multiple values is to return them as a concatenated string, and parse them when they are returned. Passing by reference is usually the best manner for simple programs (and some complex ones as well).

Reputation:

## Re: Return Multiple Values in a Function

Posted 25 June 2010 - 02:06 AM

hey when you want multiple return statment values from the function u can try this method also..
this is a way how we can return multiple values from a function.

```#include<iostream.h>
#include<conio.h>
int op(int x,int y,int &l,int & m)
{
int c;
c=x+y;
l=x-y;
m=x*y;
return c;
}
void main()
{
int a,b,*p,*q;
cin>>a>>b;
cout<<" "<<op(a,b,*p,*q);
cout<<*p;
cout<<*q;
getch();
}
```

This post has been edited by JackOfAllTrades: 25 June 2010 - 04:15 AM
Reason for edit:: Added code tags

### #41 diego_pmc

Reputation: 83
• Posts: 565
• Joined: 13-May 09

## Re: Return Multiple Values in a Function

Posted 25 June 2010 - 02:55 AM

Amadeus, on 13 April 2007 - 01:39 PM, said:

A simple manner in which to return multiple values is to return them as a concatenated string, and parse them when they are returned. Passing by reference is usually the best manner for simple programs (and some complex ones as well).

Also, if you decide to got that way it would be better to use a stringstream (#include <sstream>) -- it'll make your life easier.

### #42 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,894
• Joined: 16-October 07

## Re: Return Multiple Values in a Function

Posted 25 June 2010 - 03:49 AM

If you simply want to "return", then struct is the cleanest. However, it's often better to take the struct as a parameter, fill in the values, and return the status of whether that worked or not.

You'll need to pass the struct by reference in C++ or a pointer in C.