NO

Author Topic: Whats wrong with this code?  (Read 3296 times)

Ryuzaki33

  • Guest
Whats wrong with this code?
« 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:

Code: [Select]
#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);
}
« Last Edit: December 11, 2012, 10:12:50 PM by Stefan Pendl »

CommonTater

  • Guest
Re: Whats wrong with this code?
« Reply #1 on: December 11, 2012, 08:33:32 PM »
Code: [Select]
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...
Code: [Select]

 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...
Code: [Select]

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

Ryuzaki33

  • Guest
Re: Whats wrong with this code?
« Reply #2 on: December 13, 2012, 07:31:46 AM »
Yea....ofc...my bad.Thanks.