I know last time i had that long question stuff but just have a short one this time. Can someone show me an example of the answer of one function being sent to another? Thanks in advance!
I know last time i had that long question stuff but just have a short one this time. Can someone show me an example of the answer of one function being sent to another? Thanks in advance!
int sub ()
{
int out;
/*whatever the function is supposed to do here, and stick it in out*/
return out;
}
void main()
{
int in = sub();
/*whatever main wants to do with the answer here*/
}
So the "answer" from sub is "sent" to main. Unless I'm reading your question wrong.
Using a function to return a variable in another function for use?
Adjusted it to what I think he meant, sending to a function from a function. Another good example that would work here would be a recursive function.Originally Posted by Charla
How would I set it up to call both functions prototype in the beginning though?
No, "one function being sent to another" would be...
Edit...or maybe I misread it. What I did was sent 1 function into another.Code:bool func1(int input) { return (input > 0); } int func2() { return 1; } void main() { bool blah = func1(func2()); }
I keep getting an error on the bolded part for too many initializers ;/// Homework 4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
double tempwind (double T, double V);
double windchill (double tempwind);
int main ()
{
cout << "Enter the temperature in Fahrenheit";
double TempF;
cin >> TempF;
cout << "Enter the Wind speed";
double Wspeed;
cin >> Wspeed;
double tempwind (TempF , Wspeed);
double windchill(tempwind);
cout << windchill;
}
double tempwind (double T, double V)
{
return (35.74 + 0.6215 * T - 35.75 * pow(V, .16) + 0.4275 * T * pow(V, .16));
}
double windchill (double tempwind)
{
if (tempwind > 10)
cout <<("This is not dangerous");
else
cout <<("YOUR GONNA DIE!");
return (0);
}
I think maybe you should have taken a class in C before jumping into C++. You're confusing functions for variables...redeclaring function names instead of properly calling them and using their output, etc. These are basic technical things that are more gently taught with a procedural language.
Ah hell I'll be kind and do the job of a TA
As you learn fundamental programming you will find it best to write comments next to your code to remind yourself the intention of the function where it might otherwise not be clear since you're likely doing things like this for the first time ever. Here, I'll do it for you:Code:cout << "Enter the temperature in Fahrenheit"; double TempF; cin >> TempF; cout << "Enter the Wind speed"; double Wspeed; cin >> Wspeed; double tempwind (TempF , Wspeed); double windchill(tempwind); cout << windchill;
Coding style and readability aside, commenting like this outlines the structure of your program and is good if you ever need to identify the gap between what you're attempting to do and what you're actually asking the compiler to make happen in your program (i.e. compile-time errors).Code:cout << "Enter the temperature in Fahrenheit"; // prompt user for temperature double TempF; // stores user-entered temperature cin >> TempF; // store temperature cout << "Enter the Wind speed"; // prompt user for wind speed double Wspeed; // stores user-entered wind speed cin >> Wspeed; // store wind speed double tempwind (TempF , Wspeed); // uh...what? double windchill(tempwind); // uh...what? cout << windchill; // write...something...to output
Your windchill() function writes to output so all it needs is the value calculated by tempwind, right? I'd say more but I'd be doing your homework for you. Review what your book/notes say about calling functions and how to pass arguments to those functions.
I got it to work and it recognizes the if functions and such however the results are// Homework 4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
double tempwind (double T, double V);
double windchill (double answer);
int main ()
{
cout << "Enter the temperature in Fahrenheit";
double TempF;
cin >> TempF;
cout << "Enter the Wind speed";
double Wspeed;
cin >> Wspeed;
double answer = tempwind (TempF , Wspeed);
double Final = windchill(answer);
cout << Final;
}
double tempwind (double T, double V)
{
return (35.74 + 0.6215 * T - 35.75 * pow(V, .16) + 0.4275 * T * pow(V, .16));
}
double windchill (double answer)
{
if (answer > 10.0)
cout <<("This is not dangerous");
else
cout <<("YOUR GONNA DIE!");
return (0);
}
I'm pretty sure that 0 is there because of the return (0) is there anyway to get rid of it?Enter the temperature in Fahrenheit10
Enter the Wind speed31
YOUR GONNA DIE!0Press any key to continue . . .
It's there because you have windchill() returning a value when it doesn't have to and feeding it to cout.
I need to get paid for this kind of tutoring.
I'm pretty sure that 0 is there because of the return (0) is there anyway to get rid of it?[/quote:95c2c]Originally Posted by LinktheDeme
idk why you have a return 0 after the else. The language allows one statement after the else (without the {}) to be used as if there were {}.
So you are trying to return a double but return a 0. You normally return a double. If you do return a 0 why return at all? Do this:
Then get rid of:Code:void windchill (double answer) { if (answer > 10.0) cout <<("This is not dangerous"); else cout <<("YOUR GONNA DIE!"); }
double Final = windchill(answer);
cout << Final;
and make it:
int main()
{
//copy paste code
windchill(answer);//Only this line
}
And when you cout Final idk why you want to do that. You aren't returning useful. You are just initializing 0 to the variable and than to display it.
How do you make another function just check the values without returning a value? > >Originally Posted by Correction
Thanks btw
I don't know C/C++ can have a steep learning curve for some people.
What does a void function do?How do you make another function just check the values without returning a value? > >
Thanks btw
when i change the initial thing to voide I get errors though ;/Originally Posted by VZX
edit: Nvm forgot to change the prototype
Are you going to ask us to do every assignment?
And i'm not asking you to do anything because all you do is fucking bitch to raise your postcount when your not contributing anything useful but showing us how bitter of a person you are. I figured out the function in a function thing on my own ( I didn't understand what people helping were trying to say probably bad instructions on my part sorry) the only thing that I got help with was the void function and I thank those who helped me with it for thatOriginally Posted by aurik
edit: thanks again to those who helped
From this class, how many assignments have you done completely on your own so far?Originally Posted by LinktheDeme
5 out of 7 (including the reminder of void functions today and the one about doing multiple variable functions)Originally Posted by aurik
and out of how many posts on the topics ive made how many of them actually meant shit rather than just you PMSing?
edit: Your not even bothering to help so why the fuck are you bitching? You can easily not read the topic I'm not forcing you into them
For someone who didn't know what a function was last week, I find that hard to believe.
He can either go to his professor's office hours (some professors are shitty about being around and/or you have class during those times anyway) or consult classmates or do some research on teh interweb. One of those can easily be done at just about any time. Also, I'm pretty sure his assignment isn't "Show and example of one function being sent to another" so like, chill.