How to we return two values from a function?

J

jayapal

Hi all,

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

Thanks,
jayapal
 
J

jaysome

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
 
S

SM Ryan

# 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.
 
B

Ben Pfaff

jayapal said:
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.
 
J

jaysome

[snip]

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
 
F

Flash Gordon

Charlie Gordon wrote, On 30/10/07 07:39:
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.
 
K

ksashtekar

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;

}

In the above example you have created an instance of a struct, as a
local variable
inside a function. Will it not result in a run-time error when the
calling function tries to access the members of this struct and
the struct has actually vanished from the stack of the function
foo1() ???

Instead you can instance the struct bar1 in the calling function and
do
pass-by-reference instead !!! Also you will need to declare
the prototype for bar1 globally so that the foo1() can access it !!!
 
S

santosh

In the above example you have created an instance of a struct, as a
local variable
inside a function. Will it not result in a run-time error when the
calling function tries to access the members of this struct and
the struct has actually vanished from the stack of the function
foo1() ???

The caller will presumably assign the return value to a struct object of
type bar1.

<snip>
 
C

Chris Dollin

In the above example you have created an instance of a struct, as a
local variable inside a function.
Indeed.

Will it not result in a run-time error when the
calling function tries to access the members of this struct and
the struct has actually vanished from the stack of the function
foo1() ???

No. The calling function is handed a /copy/ of the structure value
by the `return` statement [1]. It is the compiler's business to
ensure [2] that this works as required.

[1] By the `as if` rule, it's possible that no actual copying need
be done. For example, small structs might be returned in registers,
and a cunning compiler might simply allocate `b` to those registers.
Or the caller might be required to pass the address of a place
to put the answer.

[2] Linguistic note: /not/ "insure".
 
J

James Kuyper

In the above example you have created an instance of a struct, as a
local variable
inside a function. Will it not result in a run-time error when the
calling function tries to access the members of this struct and
the struct has actually vanished from the stack of the function
foo1() ???

The problem you're thinking of only occurs when returning pointers to
local objets. It doesn't apply to returning local objects directly. If I
wrote:

struct bar1 c = foo1();

then foo1 would return the value of it's internal variable named 'b',
and that value would be copied into 'c'. It works just the same way it
would if foo1, b, and c were all defined as 'double' rather than 'struct
bar1'.
Instead you can instance the struct bar1 in the calling function and
do
pass-by-reference instead !!!

That would also work, and is the preferred method for large structures.
For small structures it's often faster or more convenient to pass around
the entire structure than to pass around a pointer to the structure.
... Also you will need to declare
the prototype for bar1 globally so that the foo1() can access it !!!

Only functions have prototypes. However, bar1's declaration should be in
scope when foo1 is defined. However, that doesn't mean that the
declaration needs to be global; if all uses of bar1 occur in the same
translation unit, the declaration only needs to be made in that
translation unit.
 
K

karthikbalaguru

In the above example you have created an instance of a struct, as a
local variable
inside a function. Will it not result in a run-time error when the
calling function tries to access the members of this struct and
the struct has actually vanished from the stack of the function
foo1() ???


Only pointers that point to local variables will get
affected/corrupted once the stack frame is deallocated.
In that case, if it is equired to return the address of
that corresponding variable , declare that with the static
storage specifier so that it will not get affected .

Here, it is returning the local variables directly and the
return type is also correct. So, No Problem.

Karthik Balaguru
 
B

blmblm

Here is the thesis itself:
http://footstool.stanford.edu/~blp/private/thesis.pdf
I am most proud of chapter 2.

Thanks to everyone for the congratulations.

Mostly-lurker momentarily popping up to say: Congratulations,
Dr. Pfaff!!!! [*]

I know you from comp.programming, where I remember being startled
to discover that you were a graduate student (i.e., not someone
in a more senior position).

[*] Normally four exclamation points would be excessive, but as
someone who has also been through the thesis-defense process,
I think this situation warrants a bit of excess.
 
J

jayapal

Charlie Gordon wrote, On 30/10/07 07:39:


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

Congratulations Ben.

whats going on .... in these thread...
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top