Thread: user-defined functions

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question user-defined functions

    Hi all. I'm writing a program to convert degrees F to degrees C by calling user-defined functions. I'm trying to get the hang of it, but I'm definately doing something wrong here. Suggestions??

    Code:
    #include <iostream>
    
    using namespace std;
    
    int GetFahrenheit();
    double ComputeCentigrade();
    double DisplayCentigrade();
    int F;
    
    int main()
    {
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << GetFahrenheit() << endl;
    	cout << "Current temperature = " << DisplayCentigrade() << endl;
    
    	return 0;
    }
    
    int GetFahrenheit()
    {
    	int F;
    
    	return F;
    }
    
    double ComputeCentigrade()
    {
    	double C;
    	int F;
    	C = (5/9) * (F-32);
    
    	return C;
    }
    
    double DisplayCentigrade()
    {
    	double C;
    
    	return C;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you trying to do here exactly with these functions? You don't pass anything to them. You don't do anything in them. All you're doing is calling a function wich returns a local value. The value you're returning hasn't actually been initialized to anything either, so it's just some random-ish value.

    If you're trying to use the C and / or F values you've declared in main, then you need to be passing those over to the function you want to use them in.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Sorry, like I said... I'm new to this. Is this getting closer? I'm still getting build errors.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int GetFahrenheit(int);
    double ComputeCentigrade(int, double);
    double DisplayCentigrade(double);
    int F;
    double C;
    
    int main()
    {
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << GetFahrenheit(F) << endl;
    	cout << "Current temperature = " << DisplayCentigrade(C) << endl;
    
    	return 0;
    }
    
    int GetFahrenheit(int F)
    {
    	return F;
    }
    
    double ComputeCentigrade(int F, double C)
    {
    	C = (5/9) * (F-32);
    
    	return C;
    }
    
    double DisplayCentigrade(double C)
    {
    	return C;
    }
    Last edited by MyntiFresh; 07-09-2005 at 11:25 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I think you're getting functions confused with class methods... this is how a simple function would look:
    Code:
    int foo(int in);
    
    int main()
    {
        int number=1;
        number=foo(1);
        return 0;
    }
    
    int foo(int in)
    {
        return in+1;
    }
    generally, you want to avoid declaring your variables globally, or in this case, outside of main().
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    The only thing I'm having trouble with now is getting the degrees C to come out correctly. Help?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int GetFahrenheit(int);
    double ComputeCentigrade(int);
    double DisplayCentigrade(double);
    
    int main()
    {
    	int F;
    	double C;
    	
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << GetFahrenheit(F) << endl;
    	cout << "Current temperature = " << DisplayCentigrade(C) << endl;
    
    	return 0;
    }
    
    int GetFahrenheit(int F)
    {
    	return F;
    }
    
    double ComputeCentigrade(int F)
    {
    	double C;
    	C = (5/9) * (F-32);
    
    	return C;
    }
    
    double DisplayCentigrade(double C)
    {
    	return C;
    }

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, clean up the mess first and you'll realize what's wrong:
    Code:
    #include <iostream>
    
    using namespace std;
    
    double ComputeCentigrade(int);
    
    int main()
    {
    	int F;
    	double C;
    	
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << F << endl;
    	cout << "Current temperature = " << C << endl;
    
    	return 0;
    }
    
    double ComputeCentigrade(int F)
    {
    	double C;
    	C = (5/9) * (F-32);
    
    	return C;
    }
    you don't have to create a function to output a variable. for example, what you were doing was sending F to a function, which did nothing but return it, unchanged. it really didn't do anything useful.
    Last edited by major_small; 07-09-2005 at 11:56 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <iostream>
    using namespace std;
    
    double ComputeCentigrade(int);
    
    int main()
    {
    	int F;
    	
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << F << endl;
    	cout << "Current temperature = " << ComputeCentigrade(F) << endl;
       
    	return 0;
    }
    
    double ComputeCentigrade(int F)
    {
    	return ((5.0f/9.0f)*(F-32));
    }
    [edit]
    Sorry didnt see smalls post ^^^
    [/edit]
    Last edited by JoshR; 07-10-2005 at 12:08 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I figured that would be easier. But I'm supposed to be able to use those three steps... baby steps so that I understand each part better. Any suggestions on how to clean things up while still using those three functions?

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, if you really want to use those other functions, go ahead... just everyplace in main() that you see C, replace with DisplayCentigrade(C), and everyplace you see F, replace with GetFahrenheit(F), except for where you declare them and assign their values... (i.e. in the cout statements only)

    even though that's not really baby steps... that's just bad programming practice...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    OK... well in that case, do you know what I'm still doing wrong with this longer way of writing it out? When I execute, the temp in C comes out weird.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int GetFahrenheit(int);
    double ComputeCentigrade(int);
    double DisplayCentigrade(double);
    
    int main()
    {
    	int F;
    	double C;
    	
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << F << "F" << endl;
    	cout << "Current temperature = " << C << "C" << endl;
    
    	return 0;
    }
    
    int GetFahrenheit(int F)
    {
    	return F;
    }
    
    double ComputeCentigrade(int F)
    {
    	double C;
    	C = (5/9) * (F-32);
    
    	return C;
    }
    
    double DisplayCentigrade(double C)
    {
    	return C;
    }

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's because the C you're outputting has never been initialized to anything. in the code you just posted, you're never calling any of the functions.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    C = (5/9) * (F-32);
    -->
    Code:
    C = (5.0/9.0) * (F-32);

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Why do this:
    Code:
    double ComputeCentigrade(int F)
    {
    	double C;
    	C = (5.0/9.0) * (F-32);
    
    	return C;
    }
    When you could do this:
    Code:
    double ComputeCentigrade(int F)
    {
    	return ((5.0/9.0)*(F-32));
    }

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I made some more changes, based on what you all have suggested. I'm still confused about something though... how do you "call" a function? Here's my code... it works but the centigrade degree doesn't come out right, and when I compile it, I don't know what to do about the error.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int GetFahrenheit(int);
    double ComputeCentigrade(int);
    double DisplayCentigrade(double);
    
    int main()
    {
    	int F;
    	double C;
    	
    	cout << "Enter temperature in Fahrenheit: ";
    	cin >> F;
    	cout << endl;
    
    	cout << "Current temperature = " << GetFahrenheit(F) << "F" << endl;
    	cout << "Current temperature = " << DisplayCentigrade(C) << "C" << endl;
    
    	return 0;
    }
    
    int GetFahrenheit(int F)
    {
    	return F;
    }
    
    double ComputeCentigrade(int F)
    {
    	return (5.0 / 9.0) * (F - 32);
    }
    
    double DisplayCentigrade(double C)
    {
    	int F;
    	C = (5.0 / 9.0) * (F - 32);
    	return C;
    }

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    GetFahrenheit(F)
    That's an example of calling a function.

    What is the error you're getting?

    The centigrade degree isn't coming out right because when you call the DisplayCentigrade function, you're passing it C, which hasn't been defined. In that same function, you use F to make a calculation. F hasn't been defined in your function either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 06:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  4. Piecewise defined discontinuous functions in Mathematica
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-16-2005, 01:24 PM
  5. functions defined as a string
    By river-wind in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 10:36 AM