C functions and data types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raddmadd
    New Member
    • Mar 2008
    • 5

    C functions and data types

    I guess I don't fully understand the way functions in C work.

    For instance, I'll use this program as an example:

    Code:
    #include <stdio.h>
    
    float celsius(int);
    
    main()
    {
    
    	int a;
    
    	for(a  = 0; a <= 300; ++a)
    		celsius(a);
    
    	return 0;
    }
    
    float celsius(int fahr)
    {
    	int a, b;
    
    	a = (5.0 / 9.0) * (fahr - 32.0);
    
    	b = printf("%d\n", a);
    	
    	return b;
    }
    So now, what I don't understand is, the data types. We declare the parameter fahr to be of the int data type. But now, what if I were to (say for instance, in the main function) put a variable as an argument for the celsius function, I don't understand how it works when say for instance, I were to declare this variable as another data type than what fahr is.

    Also, I don't understand the return statement, what if I return a variable of type int, but the function is of another data type? hm.

    Also, the data types are switched up in this program for a reason. :P
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I am not getting your question fully.
    With whatever i have understood i have given my explanation
    1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
    ==>Then the variable will be automatically converted.
    If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1

    Same is the case when returning values

    If your question is something else then post again

    Raghuram

    Comment

    • raddmadd
      New Member
      • Mar 2008
      • 5

      #3
      Thanks for the reply friend.

      Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?

      Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by raddmadd
        Thanks for the reply friend.

        Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?

        Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?

        you are specifying data type so that the compiler can allocate that many number of bytes to it.
        i cant get uyour second para fully

        Raghuram

        Comment

        • raddmadd
          New Member
          • Mar 2008
          • 5

          #5
          Well, what I was talking about in the second paragraph is...

          For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.

          I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by raddmadd
            Well, what I was talking about in the second paragraph is...

            For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.

            I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?
            Yes.
            read my previous post for more info..

            1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
            ==>Then the variable will be automatically converted.
            If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1

            Same is the case when returning values



            Raghuram

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Data types in C and C++ are broken into two families: integer and floating point.

              Integers are of different sizes (short, int, long, char, etc.) and floating point has different sizes (float, double, long double, etc.).

              With integers, any integer can be converted to any other integer and it is your responsibility to be certain the result is OK. That is, converting a larger integer to a smaller one may cause a data loss.

              With floating point, when you convert from a large one (double) to a smaller one (float), the number of signifcant figures chnages and you will get a warning about truncation an possible loss of data. This warning does not occur going th other way.

              Converting an integer to floating point results in a floating point variable with a zero decimal portion. However, due to the number of significant digits in your integer, you could see rounding and loss of accuracy.

              Converting a floating point to an integer always produces a warning about truncation and possible loss of data since integers have no decimal portion.

              When you code, try to stick with one family and do not mix integers and floating point. And when you do use floating point, unless there is a reason that you can write down on paper, use double.

              Finance and programs dealing with money should use integers. Keep the amounts in pennies. Using floating point for money (because of the nifty decimal point) will cause you trouble when the variables are rounded- and there is no way to turn that off.

              Comment

              Working...