Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Functions

Identify the error in this Swift code snippet:

func increment(_ value: inout Int) {
    value += 1
}

let number = 10
increment(&number)
ACannot pass a constant as an inout parameter.
BMissing & when calling the function.
CFunction parameter should not be inout.
DFunction must return the new value.
Step-by-Step Solution
Solution:
  1. Step 1: Check variable mutability

    The variable number is declared with let, making it a constant.
  2. Step 2: Understand inout parameter requirements

    Inout parameters require a variable that can be modified, so constants cannot be passed.
  3. Final Answer:

    Cannot pass a constant as an inout parameter. -> Option A
  4. Quick Check:

    Inout needs variable, not constant [OK]
Quick Trick: Only variables (var), not constants (let), can be inout [OK]
Common Mistakes:
  • Trying to pass constants as inout parameters
  • Forgetting & when calling function
  • Thinking function must return value when using inout

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes