Start learning free

LESSON 50 OF 348 · CALCULATIONS - CHALLENGES

Your First Kotlin Challenge: Sum of Two Numbers

Practice Read the full explanation

In this lesson we will introduce you to how challenges work and how to make the most out of them. Let's check your first coding challenge:


Sum of Two Numbers

You are given two numbers a and b and you should return their sum.

Examples:

Input: a = 4, b = 9

Output: 13

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

This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.

Each challenge gives us some input and asks us to write some code that succesfully takes that input and uses it to produce some desired output.

In this case, the input is two numbers a and b and the output is their sum.


The class:

If you look at the code editor, you will see that everything is wrapped up inside a class:

class Solution {

}

You don't need to know anything about classes at this point, we will study them in future lessons. Just leave that code untouched.


The method:

Inside this class, we will always be provided with a method. Inside this method is where we should write our solution.

In our case the method is:

public fun sum(a: Int, b: Int): Int {
    // Your solution goes here!
}

A method is just a function inside a class. When defining methods, we use visibility keywords like public in the method's header. Don't dwell on this, we will study methods in future lessons.


Parameters and return:

This method's parameters will always match the input described in the challange's statement. In our case, the parameters are a and b, the two numbers we are given.

The method should return a value which represents the desired output described in the challange's statement. In our case, it should return a + b.


Assignment:

Follow the Coding Tutorial and let's solve our first coding challenge: Sum of Two Numbers


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS - CHALLENGES

    Your First Challenge in Kotlin, explained

    Problem overview

    The challenge gives you two numbers, a and b, and asks for their sum. With a = 4 and b = 9 the answer is 13, because 4 + 9 = 13.

    The task is easy on purpose. The real goal of this lesson is to show how every challenge in the course is laid out, so that the later, harder ones feel familiar.

    How a challenge is built

    Every challenge has the same three parts: an input described in the statement, an output the statement asks for, and a function in the editor that turns one into the other. Your job is to fill in the body of that function.

    The class

    The editor wraps the function in a class:

    class Solution {
    
    }
    

    A class groups related code together. You don't need to know more than that for now. Leave the class and its name untouched.

    The method

    A function that lives inside a class is called a method. Each challenge gives you one, and the solution goes inside it. Here it is sum:

    fun sum(a: Int, b: Int): Int {
        // Your solution goes here!
    }
    

    Read the header from left to right:

    • fun starts a function.
    • sum is its name.
    • a: Int, b: Int are the parameters: the two inputs, each with a name, a colon and its type. Int is a 32-bit whole number.
    • : Int after the parentheses is the return type. It says the function must give back an Int.

    The parameters always match the input the statement describes, and the return type matches the output.

    Step by step

    1. Find the value to produce. The output is the sum of a and b, which in Kotlin is the expression a + b.
    2. Hand it back with return. A challenge is checked by what the function returns, not by what it prints. The value after return goes back to whoever called sum, and the lesson's tests compare it with the expected answer.

    The solution in Kotlin

    class Solution {
        fun sum(a: Int, b: Int): Int {
            // Write the first step below:
            return a + b
        }
    }
    

    The comment line is only a marker left by the editor; Kotlin ignores it.

    Some details of the solution:

    • The function is a single expression. Kotlin evaluates a + b first and then returns the result.
    • A return type means a return is required. Because the header promises an Int, a body that never returns one doesn't compile.
    • Visibility words are optional. You may also see the header written as public fun sum. In Kotlin, members are public unless you say otherwise, so fun sum and public fun sum mean the same thing.

    Edge cases and common mistakes

    • Printing instead of returning. println(a + b) shows the number on the console, but the function then returns nothing, and Kotlin won't compile a function with return type Int that has no return. Return the value.
    • Negative numbers and zero. They work with the same line: sum(-3, 3) is 0.
    • Overflow. An Int holds values up to 2147483647. Adding one to that wraps around to -2147483648 without any error, so sum(2147483647, 1) returns -2147483648. A challenge that expects very large sums would use Long instead. This one declares Int, so keep it.
    • Editing the class or the header. Changing the names of Solution, sum or the parameters breaks the connection with the lesson's tests.

    Testing

    Try these inputs in the editor above:

    • sum(4, 9) returns 13.
    • sum(0, 0) returns 0.
    • sum(-5, 2) returns -3.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free