Thread: To return a double value

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    To return a double value

    Hi everyone! When I take the help of pointers,by keeping the return type of the function as void,the program works fine,but when I try to pass the values to the function and use return type as double.It only displays the interger part,even after typecasting.
    Code:
    #include<stdio.h>
     double cal_per(int,int,int);
     int main()
    {
    int a=55,b=66,c=77;
    double res;
    res=cal_per(a,b,c);
    printf("%f\n",res);
    return 0;
    }
     double cal_per(int a,int b,int c)
    {
    double res;res=(a+b+c)/300.0;
    res*=100;
    return res;
    }
    


  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    It is giving the correct result. 55+66+77/3=66.

  3. #3
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Actually the answer was varying from 0.000 to the right answer. So I thought there would be some problem with returning the double value.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    The program is fine.

    But why:

    Code:
    double res;res=(a+b+c)/300.0;
    when:

    Code:
    double res = (a+b+c)/300.0;
    does exactly the same, is easier to read, and more likely to help you debug things when you *don't* initialise them properly?

    Oh, and INDENT!

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by shruthi View Post
    Actually the answer was varying from 0.000 to the right answer. So I thought there would be some problem with returning the double value.
    The answer can't vary. C does not use fuzzy logic. You wont get

    "2+2 is 5 for large values of 2 and small values of 5"

    If it varied, your program had errors. As it stands right now, the program will return 66.0
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  2. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  3. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM
  4. pow() doesnt return a double?
    By FloatingPoint in forum C++ Programming
    Replies: 23
    Last Post: 08-04-2003, 11:11 AM
  5. return as a double?
    By Dummies102 in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 08:25 PM