Pelles C forum

C language => Beginner questions => Topic started by: Ryuzaki33 on December 11, 2012, 03:31:34 PM

Title: Whats wrong with this code?
Post by: Ryuzaki33 on December 11, 2012, 03:31:34 PM
Hi all,
MY first post :)

Am not sure why am getting the following errors.The program seems fine..
Quote
C:\Users\Ryuzaki!.Vegeta-PC.000\Desktop\Programs\Lab\FunAndSwiCase.c(18): error #2168: Operands of '=' have incompatible types 'int __cdecl function(int, int)' and 'int'.
C:\Users\Ryuzaki!.Vegeta-PC.000\Desktop\Programs\Lab\FunAndSwiCase.c(18): error #2088: Lvalue required.
C:\Users\Ryuzaki!.Vegeta-PC.000\Desktop\Programs\Lab\FunAndSwiCase.c(19): error #2060: Invalid return type; expected 'int' but found 'int __cdecl (*)(int, int)'.
Heres the code:


#include<stdio.h>

int sum(int  ,int  );

int main(void)
{

    int a,b,result;
    printf("Enter two numbers\n");
    scanf("%d%d",&a,&b);
    result=sum(a,b);
    printf("The result of the two is %d\n",result);
return 0;
}

int sum(int x,int y)
{
sum=x+y;
return(sum);
}

Title: Re: Whats wrong with this code?
Post by: CommonTater on December 11, 2012, 08:33:32 PM

int sum(int x,int y)
{
   sum=x+y;
   return(sum);
}


You cannot use the function name as a variable, it is a function of type int _cdecl sum

This will work...

int sum(int x,int y)
{
   int ret;

   ret = x+y;
   return ret;
}


As a rule you should not create variables execpt when you need them so...
You can do this...

int sum(int x,int y)
{
   return x+y;
}

Title: Re: Whats wrong with this code?
Post by: Ryuzaki33 on December 13, 2012, 07:31:46 AM
Yea....ofc...my bad.Thanks.