Learn
Functions
Omitting Argument Labels

When studying Swift loops, we’ve seen underscores used in place of a variable name in a for-in loop. In functions, a similar concept applies - an underscore, (_), can be used in front of a parameter name in the function definition to allow for that parameter name to be omitted in the function call.

A parameter name in a function call is also known as an argument label.

When an _ is specified ahead of an argument label in the definition, only the argument value needs to be specified in the function call:

func funcName(_ paramName: paramType) -> returnType {
 // function body 
}

funcName(argument)  // function call 

Note: The _ has a strict placement in the function definition; it must be placed at least one space ahead of the parameter name otherwise, the compiler will throw an error.

Take a look at the following program that determines the winner of a game using each team’s final points:

let 1stTeamScore = 5 let 2ndTeamScore = 7 func findWinner(_ firstPoints: Int, secondPoints: Int) -> String { if firstPoints > secondPoints { return "Team 1 is the winner" } else { return "Team 2 is the winner" } } print(findWinner(1stTeamScore, secondPoints: 2ndTeamScore)) // Prints: Team 2 is the winner

In the program above:

  • The team scores are stored in constants at the top.
  • The function compares points using an if/else statement and determines the final winner.
  • The first parameter is omitted in the function call because of the _, meanwhile the second parameter name must be present.

Notice how omitting the first argument label in the function call results in cleaner code and less repetition.

So far in this lesson, we’ve used parameter names in function calls thus using an _ and omitting a parameter name is entirely optional. Many developers prefer this syntax because it:

  1. Improves readability
  2. Avoids repetition in the function call
  3. Relates to how functions are called in various other programming languages

Instructions

1.

In Museum.swift, we’ll write a program that will calculate the total entry price to the MoMA museum for a classroom field trip given the number of students and adults attending.

Define a function, museumEntry(), that:

  • accepts a parameter named, numAdults of type, Int, prepended by an _
  • accepts a parameter named, numStudents of type, Int, prepended by an _
  • returns a value of type, Int

Note: You will see an error in the terminal on the right, but it will go away in the next step when we populate the body of the function with code.

2.

Within the body of the function, declare the following constants to represent ticket prices:

  • studentTicket to be assigned the value, 14
  • adultTicket to be assigned the value, 25

Next, set up the following expression to determine the total price amount:

(studentTicket * numStudents) + (adultTicket * numAdults)

Store this expression in a constant, total and return total as the last line of the function.

3.

Call the function and pass in adults and students as arguments. Remember to omit the parameter names.

Wrap the function call in a print() statement to output the total price.

Folder Icon

Take this course for free

Already have an account?