Thread: Adding double numbers in a function

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    Adding double numbers in a function

    Hi all,
    When I run the following piece of code the sum that is displayed is some random large number(actually 1076101120.000000) instead of 30.0
    can anyone tell me what is going on here.

    Code:
    #include <stdio.h>
    
    int main(int argc, char argv[][])
    {
    double x=10.0;
    double y=20.0;
    double sum=0.0;
    sum=add(x,y);
    printf("\n The sum is %f\n",sum);
    }
    
    double add(double x, double y)
    {
    return (x +y);
    }
    /End

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few problems, have a look at this revised version:

    Code:
    #include <stdio.h>
    
    double add(double x, double y);
    
    int main(int argc, char *argv[])
    {
      double x=10.0;
      double y=20.0;
      double sum;
      sum=add(x,y);
      printf("\n The sum is %f\n",sum);
      return 0;
    }
    double add(double x, double y)
    {
      return (x +y);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM