How do you read two values returned by a function?

C

Clifford Stern

In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?
 
K

Kenny McCormack

In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

addsub(&g,&h);

You can figure out the rest.
 
K

Keith Thompson

In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

A function cannot return two values. The argument to your return
statement is a parenthesized comma expression; the comma operator
evaluates its left operand (discarding the result), then evaluates its
right operand, then yields the value of its right operand.

So this:
return(a, b);
is equivalent to this:
return b;

If you want to return two values from a function, you can return a
struct, or you can pass the values back via pointers.
 
M

Markus Becker

Keith Thompson said:
So this:
return(a, b);
is equivalent to this:
return b;

Not quite so. 'a' gets evaluated so this would be more equivalent to:

a;
return b;

Markus
 
S

SM Ryan

(e-mail address removed) (Clifford Stern) wrote:
# In a function that returns two values, how do you read them? Consider
# the following function:
#
# int addsub(int x,int y)
# {int a,b;
# a=x+y;
# b=x-y;
# return(a,b);}
#
# trying to read the results, for example by g,h=addsub(m,k) only gets the
# second value correctly. What is the procedure?


The comma in C is not the same as in Dikstra's notation or other
such program languages. The equivalent in C is to return a struct

typedef struct {int a,b;} Pair;
Pair addsub(int x,int y) {
Pair result;
result.a = x+y;
result.b = x-y;
return result;
}
...
Pair result = addsub(m,k);
g = result.a; h = result.b;
...
 
E

Emmanuel Delahaye

Clifford Stern wrote on 27/08/05 :
In a function that returns two values,

Not such a thing in C.
int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

Pass the addresses of the outputs or the address of a structure of the
outputs or return a structure.

/* I hate this one */
int addsub (int x, int y, int *pa, int *pb)
{
int a, b;
a = x + y;
b = x - y;

if (pa != NULL)
{
*pa = a;
}

if (pa != NULL)
{
*pa = a;
}
}

/* I like this one */
struct res
{
int a, b;
};

void addsub (int x, int y, struct res *p_res)
{
if (p_res-> != NULL)
{
p_res->a = x + y;
p_res->b = x - y;
}
}

/* not so a bad alternative as long as the size of the structure
** is not larger than one or two longs.
*/
struct res
{
int a, b;
};

struct res addsub (int x, int y)
{
struct res res;

res.a = x + y;
res.b = x - y;

return res;
}

Of course, none of these implementations are protected against
overflow...


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Clifford Stern wrote on 27/08/05 :
In a function that returns two values,

Not such a thing in C.
int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

Pass the addresses of the outputs or the address of a structure of the
outputs or return a structure.

/* I hate this one */
void addsub (int x, int y, int *pa, int *pb)
{
int a, b;
a = x + y;
b = x - y;

if (pa != NULL)
{
*pa = a;
}

if (pa != NULL)
{
*pa = a;
}
}

/* I like this one */
struct res
{
int a, b;
};

void addsub (int x, int y, struct res *p_res)
{
if (p_res-> != NULL)
{
p_res->a = x + y;
p_res->b = x - y;
}
}

/* not so a bad alternative as long as the size of the structure
** is not larger than one or two longs.
*/
struct res
{
int a, b;
};

struct res addsub (int x, int y)
{
struct res res;

res.a = x + y;
res.b = x - y;

return res;
}

Of course, none of these implementations are protected against
overflow...


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
M

Martin Ambuhl

Clifford said:
In a function that returns two values,

Your's doesn't. It returns the value of b.
Please check the FAQ before posting. That is commonly expected usenet
behavior. In this case you would have found
how do you read them?

It isn't a question of "reading" them in the case of your function. It
only returned one value. Please open your beginner's guide to C and
read about comma operators.
 
K

Keith Thompson

Markus Becker said:
Not quite so. 'a' gets evaluated so this would be more equivalent to:

a;
return b;

In general, yes, but in the program in question a and b were both
local non-volatile variables, so the evaluation of a is irrelevant.
 
D

Don

(e-mail address removed) (Clifford Stern) wrote in @news.lafn.org:
In a function that returns two values, how do you read them? Consider
the following function:

int addsub(int x,int y)
{int a,b;
a=x+y;
b=x-y;
return(a,b);}

trying to read the results, for example by g,h=addsub(m,k) only gets the
second value correctly. What is the procedure?

Your question is utter nonsense; therefore, you are a troll.

Go away.
 
C

Clifford Stern

A function cannot return two values. The argument to your return
statement is a parenthesized comma expression; the comma operator
evaluates its left operand (discarding the result), then evaluates its
right operand, then yields the value of its right operand.

So this:
return(a, b);
is equivalent to this:
return b;

If you want to return two values from a function, you can return a
struct, or you can pass the values back via pointers.
Thanks, and also to SM Ryan and Emmanuel Delahaye for the detailed
struct information.

Regarding my failure to check out the FAQ, all I can say is, "darn it, I
goofed again."

Also, I failed to reread the chapter in my book on functions carefully
enough.

Clifford Stern
(e-mail address removed)
 
C

Clifford Stern

addsub(&g,&h);

You can figure out the rest.

Thanks for that. Now I understand the crucial ability to directly affect
variables in main() by the called function.

Clifford Stern
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top