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:
funstarts a function.sumis its name.a: Int, b: Intare the parameters: the two inputs, each with a name, a colon and its type.Intis a 32-bit whole number.: Intafter the parentheses is the return type. It says the function must give back anInt.
The parameters always match the input the statement describes, and the return type matches the output.
Step by step
- Find the value to produce. The output is the sum of
aandb, which in Kotlin is the expressiona + b. - Hand it back with
return. A challenge is checked by what the function returns, not by what it prints. The value afterreturngoes back to whoever calledsum, 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 + bfirst 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, sofun sumandpublic fun summean 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 typeIntthat has no return. Return the value. - Negative numbers and zero. They work with the same line:
sum(-3, 3)is0. - Overflow. An
Intholds values up to 2147483647. Adding one to that wraps around to -2147483648 without any error, sosum(2147483647, 1)returns -2147483648. A challenge that expects very large sums would useLonginstead. This one declaresInt, so keep it. - Editing the class or the header. Changing the names of
Solution,sumor the parameters breaks the connection with the lesson's tests.
Testing
Try these inputs in the editor above:
sum(4, 9)returns13.sum(0, 0)returns0.sum(-5, 2)returns-3.