return value

Pages: 12
Halo, Can someone please tell me how to return two values at the same time on the a function call.


void conversion(int feet, int inches)
{
using namespace std;

double total_feet, final_feet, final_inches;
double const foot = 0.3048, meter = 100, inch = 12;

total_feet = feet+(inches/inch);
final_feet = total_feet*foot;
final_inches = (total_feet - final_feet)*meter;

return .....???

I want to return the value of both final_feet and final_inches.

thank you :)
There are a variety of ways, but the simplest is to simply require your users to give a reference:

1
2
3
4
void conversion( int feet, int inches, double& final_feet, double& final_inches )
{
  ...
}

BTW, this is looks like a common homework problem. If it is, you are not solving it correctly.
#include <iostream>







1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std; 
double conversion(int feet, int inches)   // void function has no return  Need to be {int,float,double ect...}
{


double total_feet, final_feet, final_inches;
double const foot = 0.3048, meter = 100, inch = 12;

total_feet = feet+(inches/inch);
final_feet = total_feet*foot;
final_inches = (total_feet - final_feet)*meter;

return final_inches, final_feet;            // to easy 
Last edited on
return final_inches, final_feet; // to easy

And wrong.


As an aside, the use of using namespace std; in the posts above smacks of voodoo coding. Learn what it means, and use it where appropriate. Sprinkling it everywhere is foolish.
Last edited on
And wrong.


so what would the correct answer b? and the book i'm learning from says to include that in basically everything. An trust me i'm not the only one round here that does that.
It's not possible to return more than one value from a function. Duoas has already posted one example of how a function can send back to the caller more than one value.

and the book i'm learning from says to include that in basically everything.
It can't be a very good book if it doesn't explain why.

An trust me i'm not the only one round here that does that.
That doesn't make it right.
@ OP: Have you learned about classes / structs?

Structs: http://www.cprogramming.com/tutorial/lesson7.html
Classes: http://www.cplusplus.com/doc/tutorial/classes/

Create an object that holds both variables and return that object from your function.

@ markyrocks: The reason your book tells you to use using namespace std; everywhere is because it's likely aimed at beginners--the author doesn't wanna overwhelm readers with the responsibility of knowing when/where to type std:: in their code. Not knocking anyone here... I'm a beginner myself.
@Cuddle:
Despite being a self-proclaimed beginner, you know the purpose of using a namespace. I tend to agree with helios that if you don't know why you're putting code into a program, you shouldn't be putting code into a program.
As an aside, the use of using namespace std; in the posts above smacks of voodoo coding. Learn what it means, and use it where appropriate. Sprinkling it everywhere is foolish.


Care to help us all out? There is nothing wrong with using using directives. I particularly like how the scope was limited to the function in this case. It would be "foolish" to never use them.

As for multiple return values, there are a number of strategies to solve this problem. The one that you're most likely after has already been posted (using non-const reference parameters). Some others include globals, non-const pointer parameters, and returning a user-defined type that encapsulates the multiple values (such as a std::pair or boost::tuple).
@ciphermagi..... so just bc someone don't know the specifics of a one particular item they should just throw there hands up an not touch the keyboard..... Bc that like saying if you don't know every square inch of c++ how dare you try to use it. With that being said i doubt 99.9% of people on this site would be programming. Well part of learning something is through practice. An memorizing library's an what there used for an different aspects like that come through practice. Shit i'm having a hard time passing an array function call but yet I should stop an read 2hrs about when an where to use namespace an why when i'm still trying to grasp the simple elements of the language?

and yes its true cuddle the book isn't trying to overwhelm with every last detail of why somthing works in the book, there trying to pass the basics. Assuming the reader knows nothing about programmin at all. I've used a similar language but all the internal functions an what not where built in. Only include files you needed where self made .ini's or other scripts. I've never heard of an array up untill a few weeks ago or a pointer. The language i used Interpreted the input without identifier types array sizes or any of that char a[]="abcdefg = $a="abcdefg"
Last edited on
That's not what I'm saying, marky. I don't expect them to understand namespaces before they ever start typing; I just think that if someone is going to tell them to do something every time they write a program, then they should explain why. Obviously cuddle knows why it's there, even though she's a beginner; the books should at least make an aside, stating that that line of code allows them to use the functions of the standard library without putting std:: in the beginning of each standard function. You don't have to know every detail of what it does, but you should know why you're doing it.
i'm just b1tchy bc i been messin with countrygirls' program for hours now an keep gettin some unresoved external err an it really makin me mad!!! my heads ready to explode.

I'm gona make a program to make my computer loop until my processor explodes an blows a hole in my floor.
i'm just b1tchy bc i been messin with countrygirls' program for hours now an keep gettin some unresoved external err an it really makin me mad!!!

Too bad there isn't some kind of forum around where you can post problems/code/questions that's frequented by knowledgeable programmers that may be able to help you :)

EDIT: Ah, I see what you mean now

http://www.cplusplus.com/forum/beginner/53986/
Last edited on
hah...
http://www.cplusplus.com/forum/beginner/54038/
This one's pissing me off.
cuddle... she's....

Not that it matters for anything, but I'm a dude... I let my girlfriend pick my handle. :)
Sorry, bro. Don't mind me, it'll probably slip my mind again. ;)
Thank you all for the input :)

@Duoas: No, i do know how to declare lots of parameters. Sorry i guess my questions wasn't clear.
So, i mean was:

i declare the function call:
double calculation(etc, etc)
{
using namespace std;
double result2, result1, blah;

result1 = blah + blah;
result2 = blah*blah;

return result1;
return result2;
}
And my question is: How do i return both result1 and result2 to the main()? so far, i only learn to return one result from the function call. Is that even possible?

And my question is: How do i return both result1 and result2 to the main()?


You can't. You can only return one object. Your options are as follows:

1) Pass result1 and result2 into the function using pointers or references, and then the function will make the changes in those variables and the changes will remain when the function ends.

prototype: void calculation(double&, double&, etc)

1
2
3
4
5
6
7
void calculation(result1, result2, etc)
{
result1 = blah + blah;
result2 = blah*blah;
// No need to return anything because the changes have been made to the original variables,
//   not to copies made just for the function.
}



2) Use a structure and return that structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct someStructure
{ double result1;
   double result2;
};

someStructure calculation(etc, etc)
{
someStructure theStructToReturn;
theStructToReturn.result1 = blah + blah;
theStructToReturn.result2 = blah*blah;

return theStructToReturn;

}


I advise you use the first option. This will require you to learn what a pointer is, what a reference is, and how to pass them into a function.

Please stop putting using namespace std; everywhere. So far I haven't seen anything that needs it; learn what it actually does, and then you'll be able to put it where it's needed.
Last edited on
use vectors:


1
2
3
4
5
6
7
8
9
10
11
12
vector<double> conversion(int feet, int inches)
{
vector<double> vec;
double total_feet, final_feet, final_inches;
double const foot = 0.3048, meter = 100, inch = 12;
total_feet = feet+(inches/inch);
final_feet = total_feet*foot;
final_inches = (total_feet - final_feet)*meter;
vec.push_back(final_feet);
vec.push_back(final_inches);
return vec;
}



to access the data use(in int main()):

1
2
3
vector<double> feet_and_inches=conversion(feet,inches);
double feet=feet_and_inches.at(0);
double inches=feet_and_inches.at(1);


oh, and dont forget:#include <vector>

i will probably get drilled by a pro for posting this...
You should not use vectors to store unlike information. Homogeneous containers should store the same type of information...

Nevertheless, you have brought me to this:
http://www.cplusplus.com/forum/beginner/18489/

Heh heh heh...
Pages: 12