# user-defined functions

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 07-10-2005
JoshR
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));`
• 07-10-2005
MyntiFresh
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? :confused: 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; }```
• 07-10-2005
JoshR
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); }```
• 07-10-2005
sean
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.
• 07-10-2005
MyntiFresh
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); }```
• 07-10-2005
laserlight
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().

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);
• 07-10-2005
MyntiFresh
Thank you!!!
• 07-10-2005
dwks
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.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12