There's a design problem with function
celsius()
.
Look at the comment in the code which describes the function:
1 2 3 4 5
|
/***************************************************************
* Celsius Function *
*This function accepts a Fahrenheit temp as an arguement and *
*returns the temp in celsius. *
***************************************************************/
|
Based upon that description I would expect the function header to look something like this:
double celsius(double num)
Whereas in fact it looks like this:
double celsius(double num1, double num2)
Now sometimes the correct response would be to alter the comment so that it correctly describes the function, maybe something like this:
1 2 3
|
* This function accepts a Fahrenheit temp and *
* some other number as arguments *
* and returns the temp in celsius. *
|
But I consider that would be to move in the wrong direction. At the moment, the function has some of its innards exposed and floating about outside. The correct approach is to keep the inner mechanisms of the function self-contained, and limit the interface with the outside world to the bare essentials which are required for it to fulfil its purpose.
In other words, change the function from this:
1 2 3 4
|
double celsius(double num1, double num2)
{
return num1*(num2-32) ;
}
|
to this:
1 2 3 4 5
|
double celsius(double num)
{
double factor = 5.0/9.0;
return factor * (num-32);
}
|
I hope you can see why this is a better approach. It makes the function self-contained, and its usage simpler. Now there is no longer any need for the calling function to have additional special knowledge of what extra parameter it is supposed to pass.