Page 1 of 2 1 2 LastLast
Results 1 to 20 of 35

Thread: Function in a function     submit to reddit submit to twitter

  1. #1
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Function in a function

    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!

  2. #2
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    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.

  3. #3
    Banned.

    Join Date
    Feb 2007
    Posts
    43
    BG Level
    1

    Using a function to return a variable in another function for use?

  4. #4
    Relic Weapons
    Join Date
    Jan 2006
    Posts
    373
    BG Level
    4

    Quote Originally Posted by Charla
    int sub ()
    {
    int out;

    /*something bizzare here, whatever you need*/
    something = sub2 ();
    return out;
    }

    int sub2 ()
    {
    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.
    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.

  5. #5
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    How would I set it up to call both functions prototype in the beginning though?

  6. #6
    Nidhogg
    Join Date
    Mar 2006
    Posts
    3,954
    BG Level
    7

    No, "one function being sent to another" would be...

    Code:
    bool func1(int input)
    {
        return (input > 0);
    }
    
    int func2()
    {
        return 1;
    }
    
    void main()
    {
       bool blah = func1(func2());
    }
    Edit...or maybe I misread it. What I did was sent 1 function into another.

  7. #7
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    // 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 keep getting an error on the bolded part for too many initializers ;/

  8. #8
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    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

    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;
    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"; // 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
    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).

    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.

  9. #9
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    // 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 got it to work and it recognizes the if functions and such however the results are

    Enter the temperature in Fahrenheit10
    Enter the Wind speed31
    YOUR GONNA DIE!0Press any key to continue . . .
    I'm pretty sure that 0 is there because of the return (0) is there anyway to get rid of it?

  10. #10
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    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.

  11. #11
    New Merits
    Join Date
    Nov 2005
    Posts
    233
    BG Level
    4

    Quote Originally Posted by LinktheDeme
    // 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);//you are initializing 0 to Final because it is being return
    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 got it to work and it recognizes the if functions and such however the results are

    [quote:95c2c]Enter the temperature in Fahrenheit10
    Enter the Wind speed31
    YOUR GONNA DIE!0Press any key to continue . . .
    I'm pretty sure that 0 is there because of the return (0) is there anyway to get rid of it?[/quote:95c2c]

    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:

    Code:
    void windchill (double answer)
    {
    	if (answer > 10.0)
    		cout <<("This is not dangerous");
    	else 
    		cout <<("YOUR GONNA DIE!"); 
    }
    Then get rid of:
    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.

  12. #12
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Quote Originally Posted by Correction
    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.
    How do you make another function just check the values without returning a value? > >

    Thanks btw

  13. #13
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    I don't know C/C++ can have a steep learning curve for some people.

    How do you make another function just check the values without returning a value? > >

    Thanks btw
    What does a void function do?

  14. #14
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Quote Originally Posted by VZX
    I don't know C/C++ can have a steep learning curve for some people.

    How do you make another function just check the values without returning a value? > >

    Thanks btw
    What does a void function do?
    when i change the initial thing to voide I get errors though ;/

    edit: Nvm forgot to change the prototype

  15. #15
    Ridill
    Join Date
    May 2005
    Posts
    13,568
    BG Level
    9

    Are you going to ask us to do every assignment?

  16. #16
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Quote Originally Posted by aurik
    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 that

    edit: thanks again to those who helped

  17. #17
    Ridill
    Join Date
    May 2005
    Posts
    13,568
    BG Level
    9

    Quote Originally Posted by LinktheDeme
    Quote Originally Posted by aurik
    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 that
    From this class, how many assignments have you done completely on your own so far?

  18. #18
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Quote Originally Posted by aurik
    Quote Originally Posted by LinktheDeme
    Quote Originally Posted by aurik
    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 that
    From this class, how many assignments have you done completely on your own so far?
    5 out of 7 (including the reminder of void functions today and the one about doing multiple variable functions)
    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

  19. #19
    Ridill
    Join Date
    May 2005
    Posts
    13,568
    BG Level
    9

    For someone who didn't know what a function was last week, I find that hard to believe.

  20. #20
    Black Belt
    Join Date
    Oct 2006
    Posts
    5,799
    BG Level
    8

    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.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Search function fails so hard
    By hagun in forum General Discussion
    Replies: 24
    Last Post: 2009-05-23, 02:54
  2. Please help w/ confusing MS Excel Functions all you smarties
    By Lbelle in forum General Discussion
    Replies: 14
    Last Post: 2008-03-26, 18:26