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


Groups > comp.lang.c > #4952

Re: returning more than one value

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: returning more than one value
Date 2011-05-28 15:45 -0700
Organization None to speak of
Message-ID <ln8vtqtpfl.fsf@nuthaus.mib.org> (permalink)
References <4de14540$0$1930$bbae4d71@news.suddenlink.net> <return-20110528210946@ram.dialup.fu-berlin.de> <4de14e85$0$1935$bbae4d71@news.suddenlink.net>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web