Variable And Function Calls On Lines By Themselves In Swift

swift

Solution

The purpose is for "Playground" Demonstrations. For example, if you put that code into playground. The window on the right will show result of the execution of the function.

If you were in a traditional project, you would likely do:

func returnFifteen() -> Int {
    var y = 10
    func add() {
        y += 5
    }
    add()
    return y
}
var someInt = returnFifteen()
println(someInt)

However, this is unnecessary in Playground:

Notice the right side.

Problem

I'm reading the iBook The Swift Programming Language and seeing a convention that I don't understand and hasn't been explained in the book: variable and functions followed by a single line with the variable or function name by itself. For example: ``` var n = 2 while n < 100 { n = n * 2 } n var m = 2 do { m = m * 2 } while m < 100 m ``` And: ``` func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y } returnFifteen() ``` What is the purpose of these lines where the variable or function name are on a line by themselves? TIA

Original source