Thread: Function Addiding Two Integers Returns Incorrect Result

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    4

    Question Function Addiding Two Integers Returns Incorrect Result

    Hello Everyone:

    I am new to functions. I would like to add two numbers input by the user. Though the math output generated in the main function works fine, "add_nums" outputs a ten digit value for something as basic as 2 + 3. I would appreciate your thoughts.

    Code:
    #include <stdio.h>
    
     /* Function Prototype */
     /*----------------------*/
     int add_nums (int x, int y);
    
     int main (void) /* begin function main */
    {
      /* Variable Declarations */
      /*-----------------------*/
      
      int num_1, num_2; 
     
      /* Greeting and objective */
      /*------------------------*/
      printf ("\n This program adds numbers using a function. \n\n");
    
      printf ("\n Please enter an integer: "); /* prompt user*/
      scanf ("%i", &num_1); /* store 1st number */
      fflush (stdin); /* clearing buffer */
      printf ("\n Enter the second integer: "); /* prompt user */
      scanf ("%i", &num_2); /* store 2nd number */
      fflush (stdin); /* clearing buffer */
      printf ("\n Adding in main you get %i.", num_1 + num_2);
      printf ("\n Adding via function you get %i.", add_nums(num_1, num_2));
    
      printf ("\n Thanks for using the program.\n\n"); /* pgm end */
    
      getchar(); /* wait for character. */
    
      return 0;
    }/* end function main */
    
    /* Function(s) */
    /*-------------*/
    int add_nums (int x, int y)
     {
      int a, b, sum;
      sum = a + b;
      return sum;
     } /* end add_nums */

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In your "add_nums()" function, variables "a" and "b" are uninitialized, so you're adding two garbage values and returning that sum.

    It looks like you want to use "x" and "y" instead (that is how you access the values passed to the function from the caller).

    Also, using "fflush()" on stdin is technically undefined behavior: FAQ > Why fflush(stdin) is wrong - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    4
    Thank you Matticus!

    I resorted to using variables a and b to get past CFree's "declaration of 'x' shadows a parameter" warning.

    Though the function add_nums has been modified, the result is incorrect. Maybe I misunderstood your suggestion? Here're the changes...

    Code:
    int add_nums (int x, int y)
    {
     int x, y, sum;
     sum = x + y;
     return sum;

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "x" and "y" are already declared in the parameter list, so you do not need to re-declare them as you're doing on line 3 of the sample you posted.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    4
    That's it! You're awesome Matticus!
    Thank you for your input...much appreciated. The following works perfectly...

    Code:
    int add_nums (int x, int y)
    {
     int sum;
     sum = x + y;
     return sum;
    } /* end add_nums */

  6. #6
    Registered Superuser nul's Avatar
    Join Date
    Nov 2014
    Location
    Earth
    Posts
    53
    Code:
    return x + y;
    "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    1
    Try this please
    Quote Originally Posted by nul View Post
    Code:
    #include <stdio.h>
     
     /* Function Prototype */
     /*----------------------*/
     int add_nums (int x, int y);
     
     int main (void) /* begin function main */
    {
      /* Variable Declarations */
      /*-----------------------*/
       
      int num_1, num_2; 
      
      /* Greeting and objective */
      /*------------------------*/
      printf ("\n This program adds numbers using a function. \n\n");
     
      printf ("\n Please enter an integer: "); /* prompt user*/
      scanf ("%i", &num_1); /* store 1st number */
    
      printf ("\n Enter the second integer: "); /* prompt user */
      scanf ("%i", &num_2); /* store 2nd number */
    
      printf ("\n Adding in main you get %i.", num_1 + num_2);
      printf ("\n Adding via function you get %i.", add_nums(num_1, num_2));
     
      printf ("\n Thanks for using the program.\n\n"); /* pgm end */
     
      getchar(); /* wait for character. */
     
      return 0;
    }/* end function main */
     
    /* Function(s) */
    /*-------------*/
    int add_nums (int x, int y)
     {
      return (x+y);
     } /* end add_nums */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atoi returns incorrect result
    By barracuda in forum C Programming
    Replies: 7
    Last Post: 04-27-2015, 08:56 AM
  2. Replies: 7
    Last Post: 03-04-2015, 05:20 PM
  3. Incorrect result in Bitwise operations
    By hjazz in forum C Programming
    Replies: 1
    Last Post: 10-30-2014, 12:27 AM
  4. incorrect result in program
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 02-15-2014, 08:46 PM
  5. Incorrect result with my program
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 03:46 PM

Tags for this Thread