Using custom argument labels

Note that the serviceCharge(mealCost:) function is not very English-like. You can add a custom label to the parameter, so the purpose of the function is easier to understand.

Modify your code as shown:

// Functions
// Computing a service charge
func serviceCharge(forMealPrice mealCost: Int) -> Int {
// serviceCharge is 10% or mealCost, which is the value returned
// forMealPrice is used when calling the function
// mealCost is used in the function itself
return mealCost / 10
}

// call the function and print to the Debug area
let serviceChargeAmount = serviceCharge(forMealPrice: 50)
print(serviceChargeAmount)

The function works exactly the same as before, but it's more readable now. Another thing you can do to make functions easier to comprehend is to use several smaller functions within a function, and these are known as nested functions. You will study that next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.187.233