usingnamespace 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;
doubleconst 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
As an aside, the use of usingnamespace std; in the posts above smacks of voodoo coding. Learn what it means, and use it where appropriate. Sprinkling it everywhere is foolish.
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.
Create an object that holds both variables and return that object from your function.
@ markyrocks: The reason your book tells you to use usingnamespace 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"
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 :)
@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.
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.
}
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 usingnamespace 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.