return from function

H

Huub

Hi,

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even possible what
I want: returning 2 integers?

Thanks.
 
V

Victor Bazarov

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even possible what
I want: returning 2 integers?

There is more than one way to skin this cat. You can return
'std::pair<int,int>', you can make those "out" arguments by passing them
by reference (or by pointer)... I would recommend the former.

V
 
M

m_r_verhage

Hi,

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even possible what
I want: returning 2 integers?

Thanks.

Hi Huub,

The previous poster is correct. This example demonstrates the "call by
reference" method to get what you want. This requires you to declare
the integers before the function is called. The function is giving te
addresses(or references) where the ints can be stored, the function
does that.

#include <stddef.h>
#include <stdio.h>

void GetTwoInts( int& a, int& b )
{
a = 3;
b = 4;
}

int main()
{
int x,y;

GetTwoInts( x, y );

printf( "x from function GetTwoInts: %d\n", x );
printf( "y from function GetTwoInts: %d\n", y );

getchar();
return 0;
}

Good luck with programming!
 
K

Kai-Uwe Bux

Huub said:
I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even possible what
I want: returning 2 integers?

You could return std::pair< int, int >. E.g.:

typedef std::pair< int, int > int_pair;

int_pair some_function ( some_arg_type some_arg ) {
...
return ( int_pair( first_int, second_int ) );
}


Best,

Kai-Uwe Bux
 
C

crea

Kai-Uwe Bux said:
You could return std::pair< int, int >. E.g.:

typedef std::pair< int, int > int_pair;

int_pair some_function ( some_arg_type some_arg ) {
...
return ( int_pair( first_int, second_int ) );
}

wow, I have been programming like 15 years and never heard of this :).
learning new...

But are you guys using rather references or you normally use "pair" if you
need to do this? Can pair s type be a reference so that it does not copy
values?
 
H

Huub

Hi Huub,

The previous poster is correct. This example demonstrates the "call by
reference" method to get what you want. This requires you to declare the
integers before the function is called. The function is giving te
addresses(or references) where the ints can be stored, the function does
that.

Thank you for the answer and example. I assume this also works when the
function returning the value is in a different file, like a class?
 
N

Nobody

wow, I have been programming like 15 years and never heard of this :).

I guess you haven't used std::map? Its iterator references a std::pair of
the key and value.
But are you guys using rather references or you normally use "pair" if
you need to do this?

It depends upon what's convenient. If the result was typically going to be
Can pair s type be a reference so that it does not
copy values?

You can't return a reference to a locally-constructed object. The storage
used for the object becomes invalid upon return.
 
W

Werner

But are you guys using rather references or you normally use "pair" if you
need to do this? Can pair s type be a reference so that it does not copy
values?

It could, but would the original still exist after the function
has exited? To what would the reference then refer (if it
was initialized with something local (in scope) to the returning
function)...?

Crash boom bang...

I haven't looked at the new standard that much, but perhaps
using move semantics can solve this. I don't think that
it would make any difference for builtin types though.

//Perhaps transfers (or moves) the contents, the only
// problem is that seeing that int has no "underlying"
// data, there may be no gain.

int_pair&& p = some_function();
 
C

crea

Nobody said:
I guess you haven't used std::map? Its iterator references a std::pair of
the key and value.

I have used sometimes, but have not needed to use iterator. Just adding
items and using them.
 
H

Huub

Hi Huub,

The previous poster is correct. This example demonstrates the "call by
reference" method to get what you want. This requires you to declare the
integers before the function is called. The function is giving te
addresses(or references) where the ints can be stored, the function does
that.

#include <stddef.h>
#include <stdio.h>

void GetTwoInts( int& a, int& b )
{
a = 3;
b = 4;
}

int main()
{
int x,y;

GetTwoInts( x, y );

printf( "x from function GetTwoInts: %d\n", x ); printf( "y from
function GetTwoInts: %d\n", y );

getchar();
return 0;
}

Good luck with programming!

Thank you. This works.
 
N

Nobody

I haven't looked at the new standard that much, but perhaps
using move semantics can solve this. I don't think that
it would make any difference for builtin types though.

AIUI, move semantics are meant for the situation where the type contains
pointers or references, allowing the copy (i.e. move) to be a shallow
copy, where "ownership" of the referenced object is transferred from the
source to the destination.

I would expect the copy to be elided in the case where the callee is
an inline function.

On some platforms, the C calling convention implements the return of
"large" objects by having the caller provide a pointer to the storage as
an additional argument, but I don't know if this is commonly done for C++
(or even if it's possible, due to issues with user-defined copy
constructors or assignment operators).
 
N

none

wow, I have been programming like 15 years and never heard of this :).
learning new...

But are you guys using rather references or you normally use "pair" if you
need to do this? Can pair s type be a reference so that it does not copy
values?

This raises at least three questions:

1- Why are you worrying that the pair get copied?
(hint: premature optimisation)

2- Why do you think that a copy actually happens rather than being
optimised away by the compiler?

3- Why do you prefer using what is an unnatural and more verbose
syntax e.g.:

---------------------------------
// Header
void some_function(some_arg_type some_arg, int &a, int & b);

// Usage
int a = 0; // Watch out if you don't pre-initialise
int b = 0;
some_function(some_arg, a, b);
----------------------------------
Over what is a more natural, less verbose and probably less error prone.

----------------------------------
// Header
typedef std::pair< int, int > int_pair;
int_pair some_function ( some_arg_type some_arg );

// Usage
int_pair answer = some_function(some_arg);
-----------------------------------

Note that while it may seem that the 2 lines in the header are worse
than the 1 line for the first case, you need three line everytime you
use the function which makes it much worse overall.

Note that the second form scales nicely to 3, 4, 5, 100 results by
using a struct as a return type or a container. The first form will
and up worse and worse.

Also, as hinted, you really should pre-initialise the response data
holder with your proposed technique. Failure to do so might hide a
very subtle bug and even then, you are relying on magic values. But
the net effect is that you code will end up writing data to a and b
twice which depending on the type of a and b might make the first
method actually slower than the second.

Yannick
 
K

Krice

I made a function that should return 2 integers. But as the compiler
indicates in "return x,y", the x is ignored. So, it is even possible what
I want: returning 2 integers?

One of the good programming practices is keep return values simple.
If you need to return two random ints you are most likely doing
something wrong.
If you need to return values like x, y (location) then the proper
way is create an user defined type/class and return it.
 
N

none

One of the good programming practices is keep return values simple.

Hmm... not in the generic sense of 'simple'.
If you need to return two random ints you are most likely doing
something wrong.

OK, as long as what you call complex return value are multiple
unrelated values, your first statement is ok.
If you need to return values like x, y (location) then the proper
way is create an user defined type/class and return it.

Exactly, then this return value can be as complex as needed (and as
simple as possible) as long as it is coherent.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top