Learn Courses My Dashboard

Lesson 9 Why do I need label

Why do I need the underscore in func totalWithTax but not in func splitBy?

struct TaxCalculator {

var tax = 0.06

func totalWithTax(_ subtotal:Double) -> Double {
    return subtotal * (1 + tax)


struct BillSplitter {

func splitBy(subtotal:Double, numPeople:Int) -> Double {
    let taxCalc = TaxCalculator()
    let totalWithTax = taxCalc.totalWithTax(subtotal)
    return totalWithTax/Double(numPeople)


let split = BillSplitter()
print(split.splitBy(subtotal: 120, numPeople: 5))


Hi Gary,

You can specify your function signature in a number of ways. For example:

func totalWithTax(_ subtotal:Double)

means that at the call site of that function it will look like you have, ie:

let totalWithTax = taxCalc.totalWithTax(subtotal)

The underscore in the function signature says to suppress the parameter name.

On the other hand this version:

func totalWithTax(subtotal:Double)

means that at the call site it will look like this:

let totalWithTax = taxCalc.totalWithTax(subtotal: subtotal)

which means that the parameter name is shown.

The other method is to provide a “label” like this:

func totalWithTax(with subtotal:Double)

and at the call site the code will look like this:

let totalWithTax = taxCalc.totalWithTax(with: subtotal)

In this case the label is displayed in place of the parameter.

Sometimes having a label can make the function read a bit easier.

I hope that makes sense.