Start learning free

LESSON 50 OF 415 · CALCULATIONS - CHALLENGES

Your First Challenge in Swift: 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 successfully 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:

func sum(_ a: Int, _ b: Int) -> Int {
    // Your solution goes here!
}

A method is just a function inside a class. Methods are defined with the func keyword. 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 challenge'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 challenge'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

    SWIFT · CALCULATIONS - CHALLENGES

    Your First Challenge in Swift, explained

    Problem overview

    The first challenge is small on purpose, because it teaches how every challenge is built. You get two numbers, a and b, and the method sum must return their sum. For a = 4 and b = 9 the answer is 13.

    How a challenge is laid out

    Each challenge has an input, the values you are given, and an output, the value your code must produce. The statement describes both and gives examples to check that you understood.

    The editor shows this starting code:

    class Solution {
        func sum(_ a: Int, _ b: Int) -> Int {
    
        }
    }
    
    • The class. class Solution is a container for the method. Leave it as it is; classes come later in the course.
    • The method. A method is a function that lives inside a class. You write the solution in its body, between the curly braces.
    • The parameters. a and b are the input. Their names match the statement, and each is an Int, a whole number.
    • The return type. -> Int says the method gives back an Int. This is the output the tests look at.

    The underscores before a and b mean that a call doesn't write argument labels: it is sum(4, 9), not sum(a: 4, b: 9).

    Approach

    There is nothing to search for or loop over. The output is the input values added together. Swift adds two Int values with the + operator, so the whole job is to compute a + b and give it back.

    Step by step

    1. Add the parameters. The expression a + b uses the two inputs directly. There is no need for a new variable.
    2. Return the result. The keyword return hands the value back to whoever called sum and ends the method. Since the header promises an Int, a method with an empty body doesn't compile, and the lesson's tests can only see something that is returned.

    The solution in Swift

    class Solution {
        func sum(_ a: Int, _ b: Int) -> Int {
            return a + b
        }
    }
    

    Swift details worth knowing:

    • Returning is not printing. The method produces a value for its caller. Nothing appears on screen unless someone prints that value, as in print(Solution().sum(4, 9)), which prints 13.
    • Single-expression shortcut. When a function body is one expression, Swift lets you drop the return keyword: a + b alone works. Writing return is clearer while you are learning.
    • No semicolons. A statement ends at the end of the line.
    • Same types only. Swift doesn't mix number types silently. Adding an Int to a Double needs an explicit conversion, but here both are Int.
    • Overflow stops the program. Int is 64 bits. If a sum goes past the largest Int, the program crashes at run time instead of wrapping around.

    Edge cases and common mistakes

    • Forgetting return. An empty body or a body with only a + b on a line among other statements doesn't give back a value.
    • Printing instead of returning. print(a + b) shows the number but returns nothing.
    • Negative numbers and zero. They work the same way: sum(-3, 3) returns 0.
    • Changing the signature. The tests call sum with the name and the types given, so keep them.

    Testing

    • a = 4, b = 9 gives 13.
    • a = -3, b = 3 gives 0.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free