Sign in to follow this  
pghTech

Returning "types" from functions

Recommended Posts

Using C++ stating your prototype such as: <type> functionname(type arg1, type arg2....) My question is, if you are passing two variables one of type float and one of type int, and you want to pass them both back, what do you specify as the "return type"? Or can you not do that? float myfunction(float n, int t) { ... ... ... return n, t } is this possible? what about one like int myfunction(char a, int n) ?

Share this post


Link to post
Share on other sites
One option is to use a std::pair<> as a return value. Ex:

std::pair<int, float> myfunction(int i, float f) {
return std::make_pair(i, f);
}

Share this post


Link to post
Share on other sites
Thanks for the advice.

So are you saying that you "can't" return to values of different types, since you're only specifying one type of return in your function protocol?

Share this post


Link to post
Share on other sites
And if you need more than 2 values, look up boost::tuple. You might want to use a boost::tuple uniformly for pairs of values too. In other cases, in makes sense to define a struct with named members and return an instance of that.

Quote:
Original post by pghTech
Thanks for the advice.

So are you saying that you "can't" return to values of different types, since you're only specifying one type of return in your function protocol?

You can't do it directly - a function maps an input to one output. But this one output can be a container that holds multiple values inside it, which is what a pair or a tuple is.

Share this post


Link to post
Share on other sites
Quote:

float myfunction(float n, int t)
{
...
...
...
return n, t
}

is this possible?


That will compile, but will not do what you think.

Quote:

So are you saying that you "can't" return to values of different types, since you're only specifying one type of return in your function protocol?


Yup. C++ does not support multiple return values. If you want a function to modify two (or more) values and get them back to the caller, you can pass them by reference, as:

void foo(int &a,float &b) { /* modify a and b */ }
// or
void foo(int *a,float *b) { /* modify *a and *b */ }


If you are not familiar with pointers or references in C++, you should look them up and also Google for "pass by reference" if that term is also unclear.

You can also return a more complex type (like std::pair or boost::tuple).

Share this post


Link to post
Share on other sites
Thanks all so much for the insight. I am familar with passing by reference as opposed by value. In my learning material, they fail to mention that "small" issue. Having worked with other "less" programming languages like VBS, that is something that wasn't a problem, so encountering it here through me, and my teaching material never pointed that out.

Thanks again,

Share this post


Link to post
Share on other sites
You can place all your various data types in a struct and return that

struct x
{
int a;
float b;

};

I have never done it before so I dont want to give you the wrong function definition but should look something like

x Function()
{

x NewStruct;
NewStruct.a=5;
NewStruct.b=1;

return NewStruct;
}

Share this post


Link to post
Share on other sites
I would seriously advise looking into boost "tuples".

The introduction of template metaprogramming to C++ has lead to the ability to write libraries that seriously extend the language.

In the case of boost tuples, we have syntax that looks like:

tuple<float, int> process( float a, int b ) {
b++;
a*=b;
return make_tuple(a, b);
}

void test(float a, int b) {
tie(a, b) = process( a, b );
}


tie makes a tuple of references to the parameters passed to it. make_tuple makes a tuple of instances of the parameters passed to it. Assignment works pairwise -- it even supports implicit type conversion in the assignment operator.

You can even skip return values:

void test2(int b) {
tie(tuples::ignore, b) = process( 1.7, b );
}


This places all 3 categories of function arguement -- input, output and inpout/output -- on an even playing field.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this