user-defined functions

This is a discussion on user-defined functions within the C++ Programming forums, part of the General Programming Boards category; Sorry for bugging you about it...but why use a temporary double (C) to store your stuff before returning it? you ...

  1. #16
    *this
    Join Date
    Mar 2005
    Posts
    498
    Sorry for bugging you about it...but why use a temporary double (C) to store your stuff before returning it? you can return the formula like so:

    This...
    Code:
       int F;
       C = (5.0 / 9.0) * (F - 32);
       return C;
    Assumming the function takes the argument C
    it then becomes this...
    Code:
       return ((5.0 / 9.0) * (C - 32));

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by sean_mackrory
    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.
    How am I supposed to define F and C. I thought I had up in "main" so I'm kinda confused. I also noticed now that I never call the function "computecentigrade". I want to use it, but only to pass it on to "displaycentigrade"... I don't want computecentigrade to output a value in the program. What do I do? Here's another slight revision.

    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;
    	double C;
    	C = (5.0 / 9.0) * (F - 32);
    	return C;
    }

  3. #18
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    int main()
    {
    	int F;
    	double C;
    You never initialize C. So it becomes a garbage value.
    Code:
    double DisplayCentigrade(double C)
    {
    	int F;
    	double C;
    	C = (5.0 / 9.0) * (F - 32);
    	return C;
    }
    You never initialize F so it becomes a garbage value.

    Your really making it harder than it is... DisplayCentigrade can be recoded to be:
    Code:
    double DisplayCentigrade(double C)
    {
    	return ComputeCentigrade(C);
    }
    Last edited by JoshR; 07-10-2005 at 10:44 AM.

  4. #19
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    In C and C++, you have 'scope'. If you declare a variable F in main, and then you declare a variable F in another function, they're two different variables. If you refer to F in a function, you're only referring to the version in that function. You could solve this problem by making them 'global' variables. You just declare them before main - maybe right after all you #include's.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thanks you guys. I really appreciate all your help. I almost have my program the way I want it. The way it is now, there's no more compiling errors or anything and it executes... but there's still something wrong with the way the degrees centigrade comes out. For example, when I input 85 for F... the corresponding C should come out to be 29, but it's coming out something like -1.6667. Suggestions???

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

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    1. Place your global int F into main().
    2. Remove your global double C.
    3. Remove "C = (5.0 / 9.0) * (F - 32);" in main().
    4. Change DisplayCentigrade(C) in main() to DisplayCentigrade(F)
    5. Change ComputeCentigrade(int) to ComputeCentigrade(double).

    EDIT:
    and another thing: in your implementation of ComputeCentigrade(), there's no need for a temporary C.
    Just return (5.0 / 9.0) * (F - 32);
    Last edited by laserlight; 07-10-2005 at 11:21 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thank you!!!

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    JoshR...

    Code:
    C = (5/9) * (F-32);
    -->
    Code:
    C = (5.0/9.0) * (F-32);
    Good point. Although, only one of the numbers has to be a double, because then the other will become one too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
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, 10: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, 12:24 PM
  5. functions defined as a string
    By river-wind in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21