-
is this funtion ok?
I am learning about functions, and so I would like your criticism (if there is anything wrong with the way I did things) - the program works, but that does not mean its right....pragmatism is not so good sometimes.....
Code:
#include <stdio.h>
float convert( float fahr_to_func );
/* fahr_func.c - kermit - August 20, 2003
* (another exercise from K & R)
* print Fahrenheit to Celsius table
* for fahr = 0, 20, ..., 300
* This version uses a function for the conversion
*/
int main(void)
{
float fahr;
float fahr_to_func;
#define LOWER 0 /* lower limit of temperature table */
#define UPPER 300 /* upper limit of temperature table */
#define STEP 20 /* step size */
printf( "\n" );
printf( "\t\t+------------------------------------------------+\n" );
printf( "\t\t| |\n" );
printf( "\t\t| Fahrenheit to Celsius Conversion Table |\n" );
printf( "\t\t| |\n" );
printf( "\t\t+------------------------------------------------+\n\n" );
for ( fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP )
{
fahr_to_func = fahr;
printf( "\t\t\t\t%3.0f\t%6.1f\n", fahr, convert( fahr_to_func ));
}
printf( "\n" );
return 0;
}
/* convert; convert Fahrenheit to Celsius */
float convert( float fahr )
{
float celsius;
celsius = ( 5.0 / 9.0 ) * ( fahr - 32.0 );
return celsius;
}
try and be nice :)
kermit
-
Looks good to me. Of course you can shorten convert() like this:
Code:
float convert( float fahr )
{
return (( 5.0 / 9.0 ) * ( fahr - 32.0 ));
}
Oh, and in the declaration of convert(), the argument name need not be specified. Eg. this should work:
float convert (float);
HTH,
-
-
Is this function ok?
Quote:
Originally posted by cc0d3r
Looks good to me. Of course you can shorten convert() like this:
Code:
float convert( float fahr )
{
return (( 5.0 / 9.0 ) * ( fahr - 32.0 ));
}
Oh, and in the declaration of convert(), the argument name need not be specified. Eg. this should work:
float convert (float);
HTH,
Ah, I like your shortened version of the function. So on a short little program like this, there will be no noticeable performance difference between my version of the function and yours, however if there were two large programs, one with functions more like I wrote, and one with functions like you wrote, would that make a marked performance difference?
And the other thing I want to know is when you mentioned about not specifiying the argument name - are you saying I can write
float convert ( float );
in the function prototype? Forgive me for this question, but I am trying to make sure of what you mean - I assume this is what you meant, but since I am still getting on to the terminology associated with the language...
-
> float convert ( float );
This would be a valid prototype.
His function will probably be faster and will also save memory because you are not using another variable.
- Sean
-
-
Don't forget to add the f on the end of your floating point numbers. If you just have 5.0 it is interpreted as a double and may cause unnecessary type casts. Use either 5.f or 5.0f to explicitly state a floating point value.
-
Ok, all of this has been quite helpful. Another question then for MrWizard:
You said to add an f to my numbers, like
5.f, or 5.0f
Would it be something like this then?
Code:
float convert( float fahr )
{
return (( 5.0f / 9.0f ) * ( fahr - 32.0f ));
}
I am assuming this is what you meant.
kermit
-
Quote:
Originally posted by kermit
Ok, all of this has been quite helpful. Another question then for MrWizard:
You said to add an f to my numbers, like
5.f, or 5.0f
Would it be something like this then?
Code:
float convert( float fahr )
{
return (( 5.0f / 9.0f ) * ( fahr - 32.0f ));
}
I am assuming this is what you meant.
kermit
Yes, that is what I had in mind.