Multiple functions with the same name

You are allowed to define two functions with the same name if they have different Types, or if they can be distinguished by their external parameter argument labels. The Type of a function is composed of the parameter Types in parentheses, followed by ->, followed by the return Type. Note that the argument labels are NOT a part of the function’s Type. (But see UPDATE below.)

For example, the following functions both have the same name and are of Type (Int, Int) -> Int:

// This:
func add(a: Int, b: Int) -> Int {
    return a + b
}

// Is the same Type as this:
func add(x: Int, y: Int) -> Int {
    return x + y
}

This will produce a compile-time error – changing the labels from a:b: to x:y: does not distinguish the two functions. (But see UPDATE below.)

Using Mr. Web’s functions as examples:

// Function A: This function has the Type (UITableView, Int) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }

// Function B: This function has the Type (UITableView, NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ... }

// Function C: This made up function will produce a compile-time error because
// it has the same name and Type as Function A, (UITableView, Int) -> Int:
func tableView(arg1: UITableView, arg2: Int) -> Int { ... }

Function A and Function B above do not conflict because they are of different Types. Function A and Function C above do conflict because they have the same Type. Changing the parameter labels does not resolve the conflict if the Types remain the same. (See UPDATE below.)

override is a different concept altogether, and I think some of the other answers cover it, so I’ll skip it.

UPDATE: Some of what I wrote above is incorrect. It is true that a function’s parameter labels are not a part of it’s Type definition, but they can be used to distinguish two functions that have the same Type, so long as the function has external labels that are different such that the compiler can tell which function you are trying to call when you invoke it. Example:

func add(a: Int, to b: Int) -> Int {    // called with add(1, to: 3)
    println("This is in the first function defintion.")
    return a + b
}

func add(a: Int, and b: Int) -> Int {   // called with add(1, and: 3)
    println("This is in the second function definition")
    return a + b
}

let answer1 = add(1, to: 3)    // prints "This is in the first function definition"
let answer2 = add(1, and: 3)   // prints "This is in the second function definition"

So, the use of external labels in a function definition will allow functions with the same name and of the same type to compile. So, it appears that you can write multiple functions with the same name so long as the compiler can distinguish them by their types or by their external signature labels. I don’t think internal labels matter. (But I hope someone will correct me if I’m wrong.)

Leave a Comment