Start learning free

LESSON 72 OF 338 · CONDITIONAL STATEMENTS

Return Negative in Go: Returning Values from if/else

Practice Read the full explanation

Create a function that takes a number as an argument and returns negative of that number. Return negative numbers without any change.

Examples:

negative(4) ➞ -4

negative(15) ➞ -15

negative(-4) ➞ -4

negative(0) ➞ 0

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Return Negative in Go, explained

    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 n is below zero, it is already what we want, so return it as it is.
    • Otherwise n is zero or positive. Flipping its sign with the unary minus, -n, gives the negative value. For zero, -n is 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

    1. Write the if. The condition is n < 0, which is true exactly when the input is already negative.
    2. Return n inside the block. No work is needed for a number that is already negative.
    3. Add an else branch 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 the if/else return, so the compiler accepts the function. If you removed the else block, the compiler would report missing return.
    • Unary minus. -n produces a new value; it doesn't change the variable n. A function parameter is a copy anyway, so changing it would not affect the caller's variable.
    • The else is optional. Because the first branch returns, the second return -n could sit directly after the if block, with no else. That is the usual Go style, and both versions behave identically. The lesson spells out the else so the two cases are easy to see.
    • Zero has no negative sign. Integers have a single zero, so -0 is just 0. The function returns 0 for an input of 0, as the example requires.

    Edge cases and common mistakes

    • Negating everything. Returning -n in both branches would turn -4 into 4, the opposite of what the task wants.
    • Zero. negative(0) returns 0. Either branch gives that result, so the choice between n < 0 and n <= 0 doesn'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 int overflows silently in Go, because its positive counterpart doesn't fit: for a variable m holding the smallest 64-bit value, -m == m is true. This function never negates a negative number, so every input is safe, and the most negative value comes back unchanged.
    • Using abs from math. Go's math.Abs works on float64, not int, so it isn't a convenient shortcut here.

    Testing

    • negative(4) returns -4.
    • negative(15) returns -15.
    • negative(-4) returns -4.
    • negative(0) returns 0.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free