Bird
0
0

You want to write a Swift function that calculates the total price including tax. The tax rate should default to 5%. How would you declare this function?

hard📝 Application Q8 of 15
Swift - Functions
You want to write a Swift function that calculates the total price including tax. The tax rate should default to 5%. How would you declare this function?
Afunc totalPrice(amount: Double = 0.05, taxRate: Double) -> Double { return amount * (1 + taxRate) }
Bfunc totalPrice(amount: Double, taxRate: Double = 0.05) -> Double { return amount * (1 + taxRate) }
Cfunc totalPrice(amount: Double, taxRate: Double) -> Double { return amount * (1 + 0.05) }
Dfunc totalPrice(amount: Double, taxRate: Double = 5) -> Double { return amount * (1 + taxRate) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct default parameter usage

    Tax rate should have a default of 0.05 (5%) and come after required parameters.
  2. Step 2: Check each option

    func totalPrice(amount: Double, taxRate: Double = 0.05) -> Double { return amount * (1 + taxRate) } correctly sets taxRate default to 0.05 and places it after amount.
  3. Final Answer:

    func totalPrice(amount: Double, taxRate: Double = 0.05) -> Double { return amount * (1 + taxRate) } -> Option B
  4. Quick Check:

    Default tax rate = 0.05 correctly set [OK]
Quick Trick: Default tax rate as decimal, parameter order matters [OK]
Common Mistakes:
  • Setting default on wrong parameter
  • Using 5 instead of 0.05
  • Ignoring parameter order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes