User defined function error

please check the code i am using this code for:
when user give 2 inputs for example user gave 1 and 3
this this should run the loop using userdefined function and add every value which comes between 1 and 3 and give result 6
but its giving garbage 1290.. please check and correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<conio.h>


int  user(int, int);
void main()
{
clrscr();

int a,b;
printf("Enter Number:"); scanf("%d",&a);
printf("\nEnter Another Number:"); scanf("%d",&b);
user(a,b);
getch();
}

int  user(int a,int b)
{
int total;
for(int c=a;c<=b;c++)
{  total=total+c;
 }
  printf("Total: %d",total);
}
Hi,

Your function doesn't return a value, so nothing happens there.

It is int main() not void main

Avoid using conio.h it's not standard.

When using scanf, make use of it's return value to see that it worked.
the same error
what should i return?
You didn't try very hard, 1 min between my post and yours. Are you trolling?
> what should i return?
¿what did you return?
¿why did you even say that you were going to return something if you have no idea of what to return?


> but its giving garbage 1290
you didn't initialise `total'
please correct the code someone :(
1
2
3
4
5
6
7
8
9
10
11
12
int  user (int a, int b)
{
  int total = 0;

  for (int c = a; c <= b; c++)
  {
    total = total + c;
  }
  printf ("Total: %d", total);

  return total;
}


Works only if a < b
Topic archived. No new replies allowed.