# How to we return two values from a function? - C

This is a discussion on How to we return two values from a function? - C ; Hi all, Is there any way to return two values from a function .................... Thanks, jayapal...

1. ## How to we return two values from a function?

Hi all,

Is there any way to return two values from a
function ....................

Thanks,
jayapal

2. ## Re: How to we return two values from a function?

On Mon, 29 Oct 2007 21:31:49 -0700, jayapal <jayapal403@gmail.com>
wrote:

>Hi all,
>
>Is there any way to return two values from a
>function ....................

Yes and no.

You can't do something like this:

int double foo(void)
{
int i = 5;
double d = 10.0;
return i d;
}

But you can do something like this:

struct bar1
{
int i;
double d;
};

struct bar1 foo1(void)
{
struct bar1 b;
b.i = 5;
b.d = 10.0;
return b;
}

/* or this: */

struct bar2
{
int i;
double d;
};

void foo2(struct bar2 *out_bar2_ptr)
{
out_bar2_ptr->i = 5;
out_bar2_ptr->d = 10.0;
}

/* or even this: */

void foo3(int *i_ptr, double *d_ptr)
{
*i_ptr = 5;
*d_ptr = 10.0;
}

Regards
--
jay

3. ## Re: How to we return two values from a function?

jayapal <jayapal403@gmail.com> wrote:
# Hi all,
#
# Is there any way to return two values from a
# function ....................

(1) Return a struct.
(2) Pass in variable addresses and assign the result thereto.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.

4. ## Re: How to we return two values from a function?

jayapal <jayapal403@gmail.com> writes:

> Is there any way to return two values from a
> function ....................

You could put the two values into a struct and return that
struct, or you could "return" one of the values by passing a
pointer to an object as one of the function's arguments.

If you can give a better description of the problem that you are
trying to solve, perhaps we can provide a more specific solution.
--
http://footstool.stanford.edu/~blp/private/out.jpeg

5. ## Re: How to we return two values from a function?

On Mon, 29 Oct 2007 21:58:37 -0700, Ben Pfaff <blp@cs.stanford.edu>
wrote:

[snip]

>--
> http://footstool.stanford.edu/~blp/private/out.jpeg

Congratulations Ben on completing your doctorate program, as noted in
the above URL that you posted in your sig. Your contributions to this
newsgroup are much appreciated. I'd hire you in a standard
CLOCKS_PER_SEC heartbeat.

Best wishes for your future and best regards
--
jaysome

6. ## Re: How to we return two values from a function?

Ben Pfaff said:

> http://footstool.stanford.edu/~blp/private/out.jpeg

Richly deserved. Congratulations!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

7. ## Re: How to we return two values from a function?

> --http://footstool.stanford.edu/~blp/private/out.jpeg

Congratulations !!

Karthik Balaguru

8. ## Re: How to we return two values from a function?

"Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de news:
yfSdnaBElsj4VLvanZ2dnUVZ8rGdnZ2d@bt.com...
> Ben Pfaff said:
>
>> http://footstool.stanford.edu/~blp/private/out.jpeg

>
>
> Richly deserved. Congratulations!

Seconded.

What was the subject of your thesis ?

--
Chqrlie.

9. ## Re: How to we return two values from a function?

Charlie Gordon wrote, On 30/10/07 07:39:
> "Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de news:
> yfSdnaBElsj4VLvanZ2dnUVZ8rGdnZ2d@bt.com...
>> Ben Pfaff said:
>>
>>> http://footstool.stanford.edu/~blp/private/out.jpeg

>>
>> Richly deserved. Congratulations!

>
> Seconded.
>
> What was the subject of your thesis ?

"The impact of hyper-pedants on programming." Why else has he been
hanging around here?

Congratulations Ben.
--
Flash Gordon

10. ## Re: How to we return two values from a function?

Charlie Gordon said:

>
> "Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de
> news: yfSdnaBElsj4VLvanZ2dnUVZ8rGdnZ2d@bt.com...
>> Ben Pfaff said:
>>
>>> http://footstool.stanford.edu/~blp/private/out.jpeg

>>
>>
>> Richly deserved. Congratulations!

>
> Seconded.
>
> What was the subject of your thesis ?

My guess: literate programming?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@