Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4917 > unrolled thread

returning more than one value

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2011-05-28 14:55 -0400
Last post2011-05-28 16:32 -0400
Articles 12 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 14:55 -0400
    Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 12:17 -0700
      Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:26 -0400
    Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:31 -0400
    Re: returning more than one value Joe Wright <joewwright@comcast.net> - 2011-05-28 15:33 -0400
      Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 15:52 -0700
      Re: returning more than one value Kenneth Brody <kenbrody@spamcop.net> - 2011-05-31 12:15 -0400
    Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 15:34 -0400
      Re: returning more than one value Keith Thompson <kst-u@mib.org> - 2011-05-28 15:45 -0700
    Re: returning more than one value Shao Miller <sha0.miller@gmail.com> - 2011-05-28 15:09 -0500
      Re: returning more than one value "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-28 16:14 -0400
    Re: returning more than one value "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-28 16:32 -0400

#4917 — returning more than one value

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 14:55 -0400
Subjectreturning more than one value
Message-ID<4de14540$0$1930$bbae4d71@news.suddenlink.net>
    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

[toc] | [next] | [standalone]


#4922

FromKeith Thompson <kst-u@mib.org>
Date2011-05-28 12:17 -0700
Message-ID<lnd3j2tz2o.fsf@nuthaus.mib.org>
In reply to#4917
"Bill Cunningham" <nospam@nspam.invalid> 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-u@mib.org  <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"

[toc] | [prev] | [next] | [standalone]


#4923

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 15:26 -0400
Message-ID<4de14ca4$0$1943$bbae4d71@news.suddenlink.net>
In reply to#4922
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

[toc] | [prev] | [next] | [standalone]


#4926

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 15:31 -0400
Message-ID<4de14db5$0$1935$bbae4d71@news.suddenlink.net>
In reply to#4917
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

[toc] | [prev] | [next] | [standalone]


#4927

FromJoe Wright <joewwright@comcast.net>
Date2011-05-28 15:33 -0400
Message-ID<poadnfx_sOmI03zQnZ2dnUVZ_jSdnZ2d@giganews.com>
In reply to#4917
On 5/28/2011 14:55, 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.
>
> Bill

A function has a type and may (or not) return a value. Functions do not 
return multiple values.

-- 
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

[toc] | [prev] | [next] | [standalone]


#4955

FromKeith Thompson <kst-u@mib.org>
Date2011-05-28 15:52 -0700
Message-ID<ln4o4etp3p.fsf@nuthaus.mib.org>
In reply to#4927
Joe Wright <joewwright@comcast.net> writes:
> On 5/28/2011 14:55, 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 has a type and may (or not) return a value. Functions do not 
> return multiple values.

But functions can return values that *contain* multiple values,
particularly by returning a struct value (see examples elsethread).

On the other hand, this technique isn't commonly used unless the
multiple values are logically part of a single entity.  It requires the
caller to create an object of the struct type to hold the result, then
extract the individual values from that object.  It's often more
convenient for the caller to pass pointers to individual variables.

Here's a program I just posted (based on Stefan Ram's program) that uses
the struct technique:

#include <stdio.h>

struct s {
    int i;
    double d;
};

struct s f( void )
{
    struct s result;
    result.i = 11;
    result.d = 12.25;
    return result;
}

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

And here's a version that doesn't use the struct:

#include <stdio.h>

void f( int *pi, double *pd )
{
    *pi = 11;
    *pd = 12.25;
}

int main( void )
{
    int i;
    double d;

    f(&i, &d);

    printf( "%d %g\n", i, d );
    return 0;
}

As you can see, a lot of the extra code to pack the information into a
struct and then unpack it is no longer necessary.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <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"

[toc] | [prev] | [next] | [standalone]


#5116

FromKenneth Brody <kenbrody@spamcop.net>
Date2011-05-31 12:15 -0400
Message-ID<wPadnQMhjoWEiXjQnZ2dnUVZ_jSdnZ2d@bestweb.net>
In reply to#4927
On 5/28/2011 3:33 PM, Joe Wright wrote:
> On 5/28/2011 14:55, 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 has a type and may (or not) return a value. Functions do not
> return multiple values.

Technically true.  But that value can be a struct, which in turn can contain 
multiple values.

-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#4928

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 15:34 -0400
Message-ID<4de14e85$0$1935$bbae4d71@news.suddenlink.net>
In reply to#4917
Stefan Ram wrote:
> "Bill Cunningham" <nospam@nspam.invalid> 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

[toc] | [prev] | [next] | [standalone]


#4952

FromKeith Thompson <kst-u@mib.org>
Date2011-05-28 15:45 -0700
Message-ID<ln8vtqtpfl.fsf@nuthaus.mib.org>
In reply to#4928
"Bill Cunningham" <nospam@nspam.invalid> writes:
> Stefan Ram wrote:
>> "Bill Cunningham" <nospam@nspam.invalid> 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.

But of course there are numerous ways to *simulate* returning multiple
values in C.  If a tutorial mentions this without showing examples, it's
probably a bad tutorial.

>>> 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?

That's exactly what the function f does.  main calls f and prints the
values.

The layout of the code makes it difficult to follow.  Here's another
version.  I've separated the declaration of the type "struct s" from the
definition of the function "f", and avoided re-using the name "s" for
multiple things.

include <stdio.h>

struct s {
    int i;
    double d;
};

struct s f( void )
{
    struct s result;
    result.i = 11;
    result.d = 12.25;
    return result;
}

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

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <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"

[toc] | [prev] | [next] | [standalone]


#4932

FromShao Miller <sha0.miller@gmail.com>
Date2011-05-28 15:09 -0500
Message-ID<irrkq1$o5h$1@dont-email.me>
In reply to#4917
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. :)

[toc] | [prev] | [next] | [standalone]


#4934

From"Bill Cunningham" <nospam@nspam.invalid>
Date2011-05-28 16:14 -0400
Message-ID<4de157e7$0$1973$bbae4d71@news.suddenlink.net>
In reply to#4932
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. :) 

[toc] | [prev] | [next] | [standalone]


#4937

From"Morris Keesan" <mkeesan@post.harvard.edu>
Date2011-05-28 16:32 -0400
Message-ID<op.vv7ko6in5qv6o3@toshiba-laptop>
In reply to#4917
On Sat, 28 May 2011 14:55:08 -0400, Bill Cunningham <nospam@nspam.invalid>  
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 -- mkeesan@post.harvard.edu

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web