Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > returning more than one value

Reply
Thread Tools

returning more than one value

 
 
Bill Cunningham
Guest
Posts: n/a
 
      05-28-2011
I came across a tutorial the other day that said a function can return
more than one value. How is this done? Pointers. Can someone please show me
an example. Say I want to return a double or an int.

Bill


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2011
"Bill Cunningham" <> writes:
>I came across a tutorial the other day that said a function
>can return more than one value. How is this done?


By using a language like Perl.

>Say I want to return a double or an int.


A double or an int is one value. You want to return
a double /and/ an int.

Here, »f« does not return two values, but one value,
which happens to be a struct:

#include <stdio.h>

struct s { int i; double d; }f( void )
{ struct s s; s.i = 11; s.d = 12; return s; }

int main( void )
{ struct s const s = f(); printf( "%d %g\n", s.i, s.d ); }

You could also call a call-back function instead of the
return to pass multiple values.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-28-2011
"Bill Cunningham" <> writes:
> I came across a tutorial the other day that said a function can return
> more than one value. How is this done? Pointers. Can someone please show me
> an example.


Isn't there an example in the tutorial?

> an example. Say I want to return a double or an int.


Don't you mean a double *and* and int? Have you misunderstood what it
means to return more than one value?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-28-2011
Keith Thompson wrote:

> Don't you mean a double *and* and int?


Oh yes.

Have you misunderstood what it
> means to return more than one value?


perhaps.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-28-2011
Stefan Ram wrote:
> You could also call a call-back function instead of the
> return to pass multiple values.


What's that mean? If I can grasp it.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-28-2011
Stefan Ram wrote:
> "Bill Cunningham" <> writes:
>> I came across a tutorial the other day that said a function
>> can return more than one value. How is this done?

>
> By using a language like Perl.
>
>> Say I want to return a double or an int.

>
> A double or an int is one value. You want to return
> a double /and/ an int.
>
> Here, »f« does not return two values, but one value,
> which happens to be a struct:
>
> #include <stdio.h>
>
> struct s { int i; double d; }f( void )
> { struct s s; s.i = 11; s.d = 12; return s; }
>
> int main( void )
> { struct s const s = f(); printf( "%d %g\n", s.i, s.d ); }
>
> You could also call a call-back function instead of the
> return to pass multiple values.


What about not using printf to print the values but using return because
you want to return a value to be taken by another function as a parameter?

Bill


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2011
"Bill Cunningham" <> writes:
>Stefan Ram wrote:
>>You could also call a call-back function instead of the
>>return to pass multiple values.

>What's that mean? If I can grasp it.


#include <stdio.h>

struct visitor
{ int( *int_result )( int );
double( *double_result )( double ); };

void f( struct visitor const * const visitor )
{ visitor->int_result( 11 );
visitor->double_result( 12 ); }

int int_queue( int const arg )
{ static int value; int result = value;
value = arg; return result; }

double double_queue( double const arg )
{ static double value; double result = value;
value = arg; return result; }

int main( void )
{ struct visitor const visitor ={ int_queue, double_queue };
f( &visitor );
printf( "%d\n", visitor.int_result( 0 ));
printf( "%g\n", visitor.double_result( 0 )); } /* prints:
11
12 */

 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      05-28-2011
On 5/28/2011 1:55 PM, Bill Cunningham wrote:
> I came across a tutorial the other day that said a function can return
> more than one value. How is this done? Pointers. Can someone please show me
> an example. Say I want to return a double or an int.


(As asked already Did the tutorial include an example?

A C function has a type which includes the specification of the type of
the returned value, if any. Thus a C function cannot sometimes return
an 'int' and sometimes a 'double'.

However, as already mentioned, you can return a struct (or a union, if
you only desire one or the other of 'double' and 'int'). Like:

union onion {
double d;
int i;
};

enum e_type {
gimme_double,
gimme_int,
e_types
};

union onion foo(enum e_type type) {
union onion value = {0};

switch (type) {
case gimme_double:
value.d = 3.14159;
break;
case gimme_int:
value.i = 42;
break;
}
return value;
}

int main(void) {
return foo(gimme_int).i;
}

Function-like macros in C can "return" any type as the macro can expand
to whatever you like, but those aren't functions, of course.

I hope this helps.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-28-2011
Shao Miller wrote:
> On 5/28/2011 1:55 PM, Bill Cunningham wrote:
>> I came across a tutorial the other day that said a function can
>> return more than one value. How is this done? Pointers. Can someone
>> please show me an example. Say I want to return a double or an int.

>
> (As asked already Did the tutorial include an example?


No I must've misread or misunderstood something. I would have to go back
over the tutorial again. It's Beej's C tutorial. I find it and his
networking tutorial interesting.

> A C function has a type which includes the specification of the type
> of the returned value, if any. Thus a C function cannot sometimes
> return an 'int' and sometimes a 'double'.
>
> However, as already mentioned, you can return a struct (or a union, if
> you only desire one or the other of 'double' and 'int'). Like:
>
> union onion {
> double d;
> int i;
> };
>
> enum e_type {
> gimme_double,
> gimme_int,
> e_types
> };
>
> union onion foo(enum e_type type) {
> union onion value = {0};
>
> switch (type) {
> case gimme_double:
> value.d = 3.14159;
> break;
> case gimme_int:
> value.i = 42;
> break;
> }
> return value;
> }
>
> int main(void) {
> return foo(gimme_int).i;
> }
>
> Function-like macros in C can "return" any type as the macro can
> expand to whatever you like, but those aren't functions, of course.
>
> I hope this helps.



 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      05-28-2011
On Sat, 28 May 2011 14:55:08 -0400, Bill Cunningham <>
wrote:

> I came across a tutorial the other day that said a function can
> return more than one value. How is this done? Pointers. Can someone
> please show me an example. Say I want to return a double or an int.


A function can only return one value as its return value, but it can
achieve the effect of multiple return values by taking pointers as
arguments and storing its "return" values in the locations pointed to
by that pointer. So you can't write anything like

//NOT VALID C
int, double myfunc(void) { return 1, 3.14159; }

void someotherfunc(void)
{
int eger; double talk;
eger, talk = myfunc();
}
//NOT VALID C


but you can "return" both an int and a double like this:

void myfunc(int *errupt, double *dutch)
{
*errupt = 3; // returning int value through "output parameter"
*dutch = 0.14159; // returning double value through "output param"
}

void sometherfunc(void)
{
int elligent;
double t;

myfunc(&elligent, &t);
printf("%d %f\n", elligent, t);
}

This technique is also useful even when you want to return just one value,
but want to have a way to signal success or failure, without reserving
any special return value to indicate failure:

/* Returns 0 for success, non-zero for failure
* Numerical result stored in *resultp
*/
int do_something(int *resultp)
{
// ...
if (/* some error condition */) return 1;
// ...
*resultp = result;
return 0;
}

--
Morris Keesan --
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
Returning more than one value from a function Mohitz C++ 11 07-03-2007 05:05 PM
returning more than one value Minkoo Seo Ruby 1 03-01-2006 11:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57