Start learning free

LESSON 144 OF 415 · LOOPS & GLOBAL VARIABLES

Array Sum in Swift: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes an array nums as argument and prints to the console the sum of all numbers in that array.

So when we called printSum([1, 2, 3]), we expected our code to print:

6

because 1 + 2 + 3 = 6. But it seems like we made some mistakes because when we run our code, it prints:

1
2
3

Assignment:

Your task is to fix our function such that it correctly computes and prints the desired sum.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · LOOPS & GLOBAL VARIABLES

    Buggy Code Array Sum in Swift, explained

    What the code is supposed to do

    The function printSum takes an array of integers, nums, and should print the sum of all its elements once. The helper mainFunction calls it with [1, 2, 3], so the program should print a single line:

    6
    

    The code you start with prints three lines instead:

    1
    2
    3
    

    The starting code is:

    func printSum(_ nums: [Int]) {
        for num in nums {
            var sum = 0
            sum += num
            print(sum)
        }
    }
    
    // Do not change this code:
    func mainFunction() {
        printSum([1, 2, 3])
    }
    

    There are two mistakes. Leave mainFunction alone and fix printSum.

    What goes wrong

    Trace the loop by hand. Everything between the braces of for num in nums runs once per element, and that includes the declaration var sum = 0.

    1. For num = 1: a new sum is created as 0, becomes 1, and 1 is printed.
    2. For num = 2: a brand-new sum is created as 0, becomes 2, and 2 is printed.
    3. For num = 3: the same happens again and 3 is printed.

    That shows both bugs at once:

    • The total is reset on every pass. A variable declared inside a loop body exists only for that one pass. Each pass starts with a fresh sum of 0, so earlier numbers are never added in.
    • The print is inside the loop. Even with a correct running total, print(sum) would run after every element and show the partial sums 1, 3 and 6. The total is complete only after the last element has been added.

    Fixing it step by step

    1. Move the declaration above the loop. Put var sum = 0 before for num in nums. It now runs once, and sum stays alive for the whole loop.
    2. Keep the addition in the loop. sum += num stays where it is, adding each element to the running total.
    3. Move the print below the loop. After the closing brace of the loop, print(sum) runs one time with the finished total.

    The fixed code in Swift

    func printSum(_ nums: [Int]) {
        var sum = 0
        for num in nums {
            sum += num
        }
        print(sum)
    }
    
    func mainFunction() {
        printSum([1, 2, 3])
    }
    

    The details that matter here:

    • Scope. A constant or variable lives from its declaration to the closing brace of the block that contains it. Declared inside the loop, sum belongs to a single pass. Declared above the loop, it belongs to the whole function body, and each pass updates the same variable. A sum declared inside the loop can't be read by a print below the loop either: the compiler reports that it cannot find sum in scope.
    • var, not let. The total changes, so it must be a var. With let, sum += num is a compile error because the constant can't be mutated.
    • Overflow. Int is 64 bits and an overflowing sum crashes the program instead of wrapping around. For this lesson the numbers are small.
    • An empty array. printSum([]) prints 0, because the loop does nothing and the total is still its starting value.

    Common mistakes

    • Fixing only one of the two bugs. Moving only the declaration prints 1, 3 and 6. Moving only the print leaves the total reset each time, so the program prints 3, the last number.
    • Moving the print but not out of the loop's braces. A print indented under the loop but before its closing brace is still inside the loop.
    • Starting the total at 1. That would make every sum one too large.
    • Changing mainFunction. The task says its code stays as it is.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free