sum(int a,int b)
{
int x;
x = a+b;
}
int main()
{
printf("%d",sum(2,3));
}
If i remove the x
then it will return first parameter always, but I am not returning any value.
sum(int a,int b)
{
int x;
x = a+b;
}
int main()
{
printf("%d",sum(2,3));
}
If i remove the x
then it will return first parameter always, but I am not returning any value.
You are trying to make use of a function call return value and the function does not have a return
statement in it.
Directly quoting C11
, chapter §6.9.1 (for functions other than main()
, Footnote)
If the
}
that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
Footnote:
From chapter §5.1.2.2.3
[...] reaching the
}
that terminates themain
function returns a value of 0.
int sum(int a,int b)
{
int x;
x = a+b;
return x;
}
int main()
{
printf("%d",sum(2,3));
}`
this is how your code should look, you're getting results sometimes as x
is a local variable and its value persists on the stack until it's overwritten, so you may end up getting the value you want, or you may end up getting some garbage