Learn

Programs can manage more than one type of data and so can a function. We’ve previously only worked with one parameter per function, but Swift functions can accept multiple input values at a time separated by commas.

Take a function, for example, that determines if a race car driver can proceed to the next round of qualifications given their current performance. Since this depends on two different types of data, the driver’s speed and state of the car, we can set up multiple parameters in a function:

func didQualify(slowestDriver: Bool, retired: Bool) -> String {

if slowestDriver || retired {
return "Eliminated"
} else {
return "Qualified"
}
}

Let’s peak under the hood:

• slowestDriver and retired are parameters of type Bool within the function, didQualify(slowestDriver:retired:) that returns a String value.
• The parameters are used in an if/else statement to determine whether or not the driver continues on to the next round.

Calling the function below, we pass in true for slowestDriver, and false for retired:

print(didQualify(slowestDriver: true, retired: false)) 

The if statement executes within the function with the Boolean values, and the functions returns "Eliminated".

Instructions

1.

In Destination.swift, set up a function, timeToDestination(), that will use an airplane’s speed and total distance to determine the duration of a flight.

• timeToDestination() should accept distance and speed parameters of type Int.

• timeToDestination() should return a value of the 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 function body, declare the constant, time and use the following formula to assign it a mathematical expression that determines the length of a flight:

$time \ = \ distance \ / speed$

Following the variable declaration, return time.

3.

Assume the passenger is flying on a superjumbo jet - the A380 from Dubai to New York! Call the function and pass in the following information about the flight:

• The distance between Dubai and New York is 6836 miles. Pass in 6836 as the argument for distance.
• The average cruising speed is 560 miles per hour. Pass in 560 as the argument for speed.

Wrap the function call in a print() statement.