Problem overview
The function negative takes an integer n and returns its negative: 4 becomes -4, and 15 becomes -15. A number that is already negative comes back unchanged, so -4 stays -4. Zero stays 0. In short, the result is never above zero, and its size is the same as the input's.
Unlike the previous lessons, this function does not print anything. It hands a value back to its caller with return, and the signature says so: func negative(n int) int. The first int is the parameter's type, and the second, after the parentheses, is the type of the result.
Approach
There are two cases, and each has a one-line answer:
- If
nis below zero, it is already what we want, so return it as it is. - Otherwise
nis zero or positive. Flipping its sign with the unary minus,-n, gives the negative value. For zero,-nis zero again, which is the answer the examples expect.
A different idea is to flip only the positive numbers, checking n > 0 first. It works too. The lesson tests the negative case first, which makes zero fall naturally into the "flip it" branch.
Step by step
- Write the
if. The condition isn < 0, which is true exactly when the input is already negative. - Return
ninside the block. No work is needed for a number that is already negative. - Add an
elsebranch that returns-n. It handles zero and every positive number. Write it as} else {on one line.
The solution in Go
func negative(n int) int {
if n < 0 {
return n
} else {
return -n
}
}
Go details that matter here:
- Every path must return. A function with a result type has to end in a
return(or another terminating statement). Here both branches of theif/elsereturn, so the compiler accepts the function. If you removed theelseblock, the compiler would reportmissing return. - Unary minus.
-nproduces a new value; it doesn't change the variablen. A function parameter is a copy anyway, so changing it would not affect the caller's variable. - The
elseis optional. Because the first branch returns, the secondreturn -ncould sit directly after theifblock, with noelse. That is the usual Go style, and both versions behave identically. The lesson spells out theelseso the two cases are easy to see. - Zero has no negative sign. Integers have a single zero, so
-0is just0. The function returns0for an input of 0, as the example requires.
Edge cases and common mistakes
- Negating everything. Returning
-nin both branches would turn -4 into 4, the opposite of what the task wants. - Zero.
negative(0)returns0. Either branch gives that result, so the choice betweenn < 0andn <= 0doesn't change the output. - Printing instead of returning.
fmt.Println(-n)shows the value on the console but gives the caller nothing back. The lesson's tests look at the returned value. - Overflow at the extreme. Negating the smallest
intoverflows silently in Go, because its positive counterpart doesn't fit: for a variablemholding the smallest 64-bit value,-m == mistrue. This function never negates a negative number, so every input is safe, and the most negative value comes back unchanged. - Using
absfrommath. Go'smath.Absworks onfloat64, notint, so it isn't a convenient shortcut here.
Testing
negative(4)returns-4.negative(15)returns-15.negative(-4)returns-4.negative(0)returns0.