Here's an example of a sum function in Swift that has a bug:
main.swift320 chars12 lines
In this example, the sum function takes an array of Int values and returns the sum of those values. However, there is a bug in the implementation, where the result variable gets modified by adding 1 to it, resulting in a wrong total. The code compiles without errors, so it is not immediately clear that there is a bug in the function. The bug can be fixed by simply removing the + 1
on the return statement.
gistlibby LogSnag