6 Replies - 117087 Views - Last Post: 25 June 2010 - 03:49 AM Rate Topic: ***** 1 Votes

#1 freshman_prog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • 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. :)
Was This Post Helpful? 1

#38 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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);
}

Was This Post Helpful? 5

#39 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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).
Was This Post Helpful? 0
  • +
  • -

#40 Guest_Parkash Goswami*


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

Was This Post Helpful? 0

#41 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Return Multiple Values in a Function

Posted 25 June 2010 - 02:55 AM

View PostAmadeus, 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.
Was This Post Helpful? 0
  • +
  • -

#42 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • 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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1