Start learning free

LESSON 51 OF 338 · CALCULATIONS - CHALLENGES

Sum of Two Numbers in Go: Your First Challenge

Practice Read the full explanation

In this lesson we will introduce you to how coding challenges work and how to make the most out of them.

Sum of Two Numbers

You are given two numbers a and b. Return their sum.

Example:

Input: a = 4, b = 9
Output: 13
Explanation: The sum of a and b is 4 + 9, which is 13.

The function

In Go, the starter code gives you a function with parameters. The parameters represent the challenge input:

func sum(a int, b int) int {
    // Your code goes here
}

The int after the parameter list means the function must return an integer.

For this challenge, return a + b.

Assignment
Follow the Coding Tutorial and return the sum of the two inputs.

STEP 1 OF 1
    Ctrl + Enter to check

    GO · CALCULATIONS - CHALLENGES

    Your First Challenge in Go, explained

    Problem overview

    You are given two integers, a and b, and the function must return their sum. For a = 4 and b = 9 the answer is 13.

    The task is deliberately tiny. Its purpose is to show how a coding challenge works: you get a function with parameters, the parameters are the input, and you return the answer.

    Reading the function

    The starter code gives you the function signature:

    func sum(a int, b int) int {
        // Write the first step below:
    
    }
    
    • func sum declares a function named sum.
    • (a int, b int) lists two parameters. Each one has a name followed by its type, and both are int. When the lesson's tests run, they call sum with different pairs of numbers and the parameters hold those values.
    • The int after the parameter list is the result type. It tells Go that the function must hand back an integer.

    Go is strict about this. A function with a result type has to end with a return that supplies a value of that type. With the body left empty, the code does not compile and reports missing return.

    How to approach any challenge

    Every challenge in the course follows this pattern, so it helps to build a routine. Read the statement and the example until you can say the task in one sentence. Look at the parameters, since they are your input, and at the result type, since it tells you what to produce. Work the example by hand: here, 4 plus 9 is 13. Only then write code, and run it against the example before you submit.

    Step by step

    There is only one step: inside sum, return the sum of a and b. Add the parameters with the + operator and put return in front. The return keyword hands the value back to whoever called the function and ends the call.

    The solution in Go

    func sum(a int, b int) int {
        // Write the first step below:
        return a + b
    }
    

    A few details about these lines:

    • a + b is an expression, so return a + b returns the computed value without needing a variable to hold it. You could write total := a + b and then return total, with the same result.
    • Go does not convert between number types on its own. The sum of two int values is an int, which matches the declared result. Adding an int to a float64 would be a compile error.
    • Go also allows func sum(a, b int) int, where parameters of the same type share one type name. The lesson uses the longer form, and both mean the same thing.
    • On this platform an int has 64 bits. If the true sum is beyond the range of an int, the result silently wraps around instead of raising an error. That is far from the sizes in this challenge.

    Complexity

    The function does one addition, whatever the values are, so it takes constant time, O(1), and constant extra space.

    Edge cases and testing

    Try a few inputs in the editor above, calling the function with your own values:

    • sum(4, 9) gives 13, the lesson's example.
    • sum(-3, 3) gives 0. Negative numbers need no special handling, because + works on them directly.
    • sum(0, 0) gives 0.

    The most common beginner mistakes are leaving the function body empty, which gives missing return, and writing a + b without return, which Go rejects with an error saying the expression is not used. A result only reaches the caller through return.

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free