C / C++ FAQs & Programming Resources - ProkutFAQ : ReturningMultipleValues

HomePage Recent Changes Recently Commented Login/Register

Revision [749]

Most recent edit made on 2008-10-21 11:05:37 by SharathAV

Additions:

CategoryPointers




Revision [646]

Edited on 2007-11-13 10:29:06 by SharathAV

Deletions:
eltdronbo




Revision [578]

Edited on 2007-11-11 03:10:23 by AcpasDomco (unregistered user)

Additions:
eltdronbo




Revision [355]

Edited on 2007-04-26 09:37:42 by SharathAV [Added the method to return structure]

Additions:

How can we return more than one value from a function?


One simple way is to embed the different values that we want to return in a struct and then return the struct.
Example:
struct data
{
int a;
float b;
}

struct data myFunction(void)
{
struct data x;
/*set values for the members of x*/
return x;
}

int main()
{
struct data myData;
myData=myFunction();
/*Now you can access the different values within myData structure*/
return 0;
}


The other way around is to pass the pointers to different objects as argument to the function.
Suppose, you need a function fun1() to return one integer value and three float values, then, declare fun1() as:
int fun1(float *x, float *y, float *z);


Example:
int fun1(float *x, float *y, float *z)
{
int status;
/*Set the values of *x, *y, *z and status*/
return status;
}


While calling the function, pass address of three float variables in which you want to store the returned values, like:

x = fun1(&float1, &float2, &float3);


Where float1, float2, float3 are float variables and x is an integer.

References

comp.lang.c FAQ:How can I return multiple values from a function?


Deletions:
More than one values can be returned using pointers.
Suppose, you need a function fun1() to return one integer value and three float values, then, declare fun1() as
int fun1(float *x, float *y, float *z);
While calling the function, pass address of three float variables in which you want to store the returned values, like
= fun1(&float1, &float2, &float3); /* where float1, float2, float3 are float variables... */
Now, in function fun1, give values to the addresses passed using * operator...
*x=a;
*y=b;
*z=c; /* where a,b,c are floats declared in fun1. */
Now return the integer value in usual manner using 'return'.
In this way, one can make a function return multiple values...




Revision [354]

Edited on 2007-04-20 10:16:25 by TanayGoel

Additions:
= fun1(&float1, &float2, &float3); /* where float1, float2, float3 are float variables... */
*z=c; /* where a,b,c are floats declared in fun1. */


Deletions:
= fun1(&float1, &float2, &float3); where float1, float2, float3 are float variables...
*z=c;
where a,b,c are floats declared in fun1.




Revision [353]

Edited on 2007-04-20 10:14:31 by TanayGoel

Additions:
More than one values can be returned using pointers.
Suppose, you need a function fun1() to return one integer value and three float values, then, declare fun1() as
int fun1(float *x, float *y, float *z);
While calling the function, pass address of three float variables in which you want to store the returned values, like
= fun1(&float1, &float2, &float3); where float1, float2, float3 are float variables...
Now, in function fun1, give values to the addresses passed using * operator...
*x=a;
*y=b;
*z=c;
where a,b,c are floats declared in fun1.
Now return the integer value in usual manner using 'return'.
In this way, one can make a function return multiple values...


Deletions:




Revision [352]

Edited on 2007-04-20 10:05:06 by TanayGoel

Deletions:
One can return multiple values from a function using pointers. For example,
#include<iostream>
using namespace std;
void multiple_returns(int *, int *, int *);
int main()
{
int a, b, c;
multiple_returns(&a,&b,&c);
cout
a
"\t"
b
"\t"
c;
getchar();
return 0;
}
void multiple_returns(int *x, int *y, int *z)
{
int p,q,r;
cout
"Enter values for p, q, r : ";
cin
p
q
r;
*x=p;
*y=q;
*z=r;
}
In this example, function multiple_returns is "returning" three values although being declared as 'void'...




Revision [351]

The oldest known version of this page was edited on 2007-04-20 10:03:42 by TanayGoel
One can return multiple values from a function using pointers. For example,

#include<iostream>
using namespace std;
void multiple_returns(int *, int *, int *);
int main()
{
int a, b, c;
multiple_returns(&a,&b,&c);
cout
a
"\t"
b
"\t"
c;
getchar();
return 0;
}
void multiple_returns(int *x, int *y, int *z)
{
int p,q,r;
cout
"Enter values for p, q, r : ";
cin
p
q
r;
*x=p;
*y=q;
*z=r;
}

In this example, function multiple_returns is "returning" three values although being declared as 'void'...
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.1417 seconds