is this funtion ok?

This is a discussion on is this funtion ok? within the C Programming forums, part of the General Programming Boards category; I am learning about functions, and so I would like your criticism (if there is anything wrong with the way ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    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

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    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,
    $ENV: FreeBSD, gcc, emacs

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Good job. Looks fine.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Is this function ok?

    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...

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    > 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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    What he ^^ said.
    $ENV: FreeBSD, gcc, emacs

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write funtion and CR/LF issue
    By Yerak in forum C Programming
    Replies: 5
    Last Post: 05-05-2007, 01:19 PM
  2. locate funtion
    By DanC in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2006, 02:21 PM
  3. Funtion pointer in structure
    By Xzyx987X in forum C Programming
    Replies: 1
    Last Post: 07-03-2004, 03:05 AM
  4. HELP!!! parameters of funtion???????
    By WildLyxn in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 10:19 PM
  5. swap funtion
    By ManicC in forum C Programming
    Replies: 9
    Last Post: 11-15-2001, 06:55 AM

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