Thread: is this funtion ok?

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

    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,708
    Good job. Looks fine.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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

    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,534
    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, 03: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, 11:19 PM
  5. swap funtion
    By ManicC in forum C Programming
    Replies: 9
    Last Post: 11-15-2001, 07:55 AM