2

When a function is called, I don't need to label input values. When a method inside of a class is called, I'm asked to label only the second input. Why?!

func addVals(value1: Int, value2: Int) -> Int{
    println(value1)
    println(value2)
    let value3 = value1 + value2
    println(value3)
    return value3
}

let val1 = 1
let val2 = 6
// this works with no labels
addVals(val1, val2) // evals to 7

// in a class
class testClass {
    func addVals(value1: Int, value2: Int) -> Int{
        println(value1)
        println(value2)
        let value3 = value1 + value2
        println(value3)
        return value3
    }

    func callAddVals() -> Int {
        let val1 = 35
        let val2 = 46
        // only second input needs label??!!
        return addVals(val1,value2: val2)
    }
}
ProGirlXOXO
  • 2,170
  • 6
  • 25
  • 47
  • 1
    Learn Objective-C and you will understand. – Hot Licks Feb 23 '15 at 01:01
  • More related questions: http://stackoverflow.com/questions/24680771/using-function-parameter-names-in-swift/24681131 and http://stackoverflow.com/questions/24045890/why-does-a-function-call-require-the-parameter-name-in-swift?rq=1 – Mick MacCallum Feb 23 '15 at 01:19

1 Answers1

4

This is so that method names can read like natural language. Take a look:

mySpriteNode.runAction(dance, withKey:"NodeDance")

The argument label withKey is nice to make it clear what that string is, but see the redundancy in the below version?

mySpriteNode.runAction(action:dance, withKey:"NodeDance")

There's no need for the first argument to have a label; it can be included (more expressively!) in the method name.

andyvn22
  • 14,696
  • 1
  • 52
  • 74