You declared that the SUM
function will return an integer value (int
).
Then you added two float
s together, and returned the address of the result.
The address of a float
is nothing like the value of an int
.
You have invoked Undefined Behavior.
float SUM (float n1, float n2); // SUM should return a float.
float SUM (float n1, float n2) {
float sum = n1+n2;
return sum; // Return the sum (without any addresses)
}
P.S.
You have a function named SUM
(with capital letters), and a variable named sum
with lowercase letters. While this is technically OK, it may be a point of confusion if another programmer reads your code.
Can you always be sure of the difference between Sum
, sum
, SUM
? What about Sine
, sine
, sin
, Sin
, SINE
, etc.?
You should put in extra effort to name your functions, variables, constants, etc clearly and unambiguously so that understanding and using the proper one in the proper context is easy for all programmers.