# Return Values

• 04-09-2003
Blanket
Return Values
I was told that when returning a value from a function, you can only return one. So why does this code work? And can you explain what happens in the functions? thanks

Their is a structure called Distance that includes distances in feet and inches. the user enters 2 distances (feet,inches) and stores them in 2 type Distance variables. then this function is involked to return the larger of the 2 Distances;
Code:

```Distance bigengl(Distance dd1, Distance dd2)         {         if(dd1.feet > dd2.feet)      //if feet are different, return                 return dd1;                // the one with the largest feet         if(dd1.feet > dd2.feet)                 return dd2;         if(dd1.inches > dd2.inches)  //if inches are different,                 return dd1;                //return one with largest         else                          //inches, or dd2 if equal                 return dd2;         }```
• 04-09-2003
Luigi
the return type depends on what kind of function you use..

int main for example return ant int (duh!) 0 for all ok everything else somethin went wrong..

void no return at all

char returns char..
double returns double..
etc..

Code:

```Distance bigengl(Distance dd1, Distance dd2)         {         if(dd1.feet > dd2.feet)      //if feet are different, return                 return dd1;                // value in dd1 if(dd1.feet > dd2.feet)                 return dd2;              // return value in dd2         if(dd1.inches > dd2.inches)  //if inches are different,                 return dd1;                //return value of dd1 else                          //inches, or return value of dd2 if equal                 return dd2;         }```
• 04-10-2003
ripper079
If think you are confused by all the return statments in your function. You can have as many return statments as you want, but I would advise you to have as few as possible (there is a little performence hit if you have many return statements). Only one return statement is executed and therefore only one object (a Distance object in your case) is returned.

Quote:

And can you explain what happens in the functions?
It returns the largest Distance object

Well I think the line
Code:

`if(dd1.feet > dd2.feet)//second if clause`
should be
Code:

`if(dd1.feet < dd2.feet)`
or else it donīt makes any sense.

I donīt know if you have covered references yet but if you have I would advise you to have const refernces as your argument (dd1, dd2). Use this to avoid unneccesary copies of object(s). When object(s) gets rather big you will get a BIG perfomance hit.
• 04-10-2003
Speedy5
Quote:

#include <Jesus.h>
It will save your life
I like! I like! Very original.
• 04-10-2003
sikamikaniko
yes, the function is returning only one variable. But it depends on the case which one to return. It will return only the one where the IF operator is true or if no IFs are tru, then it will return the last return; :)

P.S.: You can return more than one variable from a function using pointers...