Problem overview
The function absDiff receives two integers, a and b, and returns the distance between them as a number that is never negative. This is the absolute value of a - b, computed without calling an absolute-value function.
For a = 10 and b = -1, the difference a - b is 11. For a = -5 and b = -7, it is 2. In both cases the order of the arguments does not change the answer: absDiff(3, 0) and absDiff(0, 3) both return 3.
Approach
One way is to compute a - b and, if the result is negative, flip its sign. A shorter route is to decide which argument is larger and subtract the smaller from it. The result of "larger minus smaller" can never be negative, so no sign fix is needed. Only one comparison, a < b, tells you which of the two is smaller. That is why the lesson uses it, and why it needs just one if and no helper variables.
Step by step
- Compare the two numbers. Write an
ifwith the conditiona < b, with braces and no parentheses. - Handle the case where
ais smaller. Inside theif,return b - a. Herebis the larger number, so the result is not negative. - Handle everything else after the block. Below the
if,return a - b. When theifwas true, the function has already returned, so this line runs only whenais greater than or equal tob. Thena - bis not negative. When the two are equal, it is zero.
The solution in Go
func absDiff(a int, b int) int {
if a < b {
return b - a
}
return a - b
}
Notes on the Go details:
- No else needed. A
returnends the function immediately, so code after anifthat returns runs only when the condition was false. This early-return shape avoids a nestedelse, and it is common in Go. - The final return. Go requires the function to end with a return statement, which the last line provides. Without it, the compiler reports
missing return. - Parameter list.
a int, b intcan also be writtena, b int. The lesson's form repeats the type for each parameter, and both are equivalent. - Integer types. The signature uses
int, so decimal arguments such as1.5are not accepted. A version for decimals needsfloat64parameters and afloat64result.
Tracing two calls
For absDiff(0, 3), the test 0 < 3 is true, so the function returns 3 - 0, which is 3. For absDiff(10, -1), the test 10 < -1 is false, so the if body is skipped and the function returns 10 - (-1), which is 11. Each call reaches exactly one return.
Complexity
The function does one comparison and one subtraction, so time is O(1) and extra space is O(1).
Edge cases and common mistakes
- Equal numbers.
absDiff(6, 6)goes to the final return and gives0. Usinga <= bin the condition would give the same answer, sinceb - ais also zero. - Opposite signs.
absDiff(6, -6)returns12: subtracting a negative number adds its size. The branch is chosen bya < balone, so no special case for signs is needed. - Both negative.
absDiff(-5, -7)is2. Hereais larger thanb, so the function returnsa - b, which is-5 - (-7). - Always subtracting in one order. Returning only
a - bis negative forabsDiff(0, 3), which gives-3. - Overflow. Integers wrap silently in Go. With extreme values such as
absDiff(9223372036854775807, -1), the true difference does not fit in anint, and the function returns-9223372036854775808. Ordinary inputs never get near this.
Testing
absDiff(3, 0)gives3.absDiff(0, 3)gives3.absDiff(10, -1)gives11.absDiff(-5, -7)gives2.absDiff(6, -6)gives12.