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 */