Problem overview
The function absoluteValue receives an int, n, and returns its absolute value: the number without its sign, or equivalently its distance from zero. For -5 the answer is 5, for 250 it is 250, and for 0 it is 0.
The task is to build this with a plain condition rather than a library function. Go's standard library has math.Abs, but it works on float64 values, not on int, so for integers you write the check yourself anyway.
Approach
Only negative numbers need to change. A number that is zero or positive is already its own absolute value. For a negative number, negating it flips the sign: -(-5) is 5. So the function has two cases, split on whether n is below zero, and each case returns directly.
Step by step
- Test for a negative number. Write
if n < 0. Go takes no parentheses around the condition, and the braces are required. - Return the negation. Inside the
if,return -nhands back the positive version of the number. - Return the number unchanged otherwise. The
elsebranch covers zero and every positive number, and returnsnas it is. Zero belongs here, which is correct because its absolute value is zero.
The solution in Go
func absoluteValue(n int) int {
if n < 0 {
return -n
} else {
return n
}
}
Some details for Go:
- Every path returns. A function with a result type must end in a return on every route. Here both the
ifand theelsereturn, and the compiler accepts that as a complete function. If you left out theelsebranch and its return, the function could reach its closing brace without returning, and the compiler would reportmissing return. - Unary minus.
-nnegates the value ofnand leaves the variable itself unchanged. - Layout of else.
} else {must stay on one line;elseon a new line is a syntax error. - Style. Because the
ifbranch returns, Go programmers often drop theelseand write the second return after the block, as inif n < 0 { return -n }followed byreturn n. Both behave the same. - Integer types. The function works for
intonly. Afloat64argument needs a separate function withfloat64in its signature, because Go does not convert between numeric types automatically.
Tracing two calls
For absoluteValue(-5), the test -5 < 0 is true, so the function returns -(-5), which is 5, and the else branch is never looked at. For absoluteValue(3), the test 3 < 0 is false, so execution jumps to the else branch and returns 3. Exactly one of the two returns runs on every call.
Edge cases and common mistakes
- Zero.
absoluteValue(0)returns0. Usingn <= 0would also work, since-0is0, but< 0says what you mean. - Negating everything. Returning
-nin both branches would turn positive numbers negative. - The smallest integer. An
intis 64 bits, and its smallest value is-9223372036854775808. Its positive counterpart does not fit, so negation wraps around and gives back the same negative number. Running it prints-9223372036854775808for that input. Ordinary tests never reach this, but it is the one case where an integer absolute value cannot be right. - Floating-point inputs. This function takes an
int. For decimals such as-0.14, the sameiflogic works in a function declared withfloat64and returns0.14. - Missing return. Forgetting a return in one branch causes the
missing returncompile error rather than a wrong answer.
Testing
absoluteValue(-5)gives5.absoluteValue(7)gives7.absoluteValue(0)gives0.absoluteValue(-1)gives1.