Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q6 of 15
Swift - Functions

Identify the error in this Swift code snippet:

func addTen(_ number: inout Int) {
    number += 10
}

addTen(5)
ACannot pass a literal to an inout parameter
BCannot use inout with Int type
CFunction must return a value
DMissing & when passing the argument
Step-by-Step Solution
Solution:
  1. Step 1: Check the function call argument

    The function expects an inout Int parameter, which requires a variable passed with &.
  2. Step 2: Identify the problem with passing a literal

    Passing a literal like 5 directly is invalid because inout requires a variable that can be modified.
  3. Final Answer:

    Cannot pass a literal to an inout parameter -> Option A
  4. Quick Check:

    Inout needs a variable, not a literal [OK]
Quick Trick: Only variables can be passed as inout parameters [OK]
Common Mistakes:
  • Passing constants or literals instead of variables
  • Forgetting & when calling the function
  • Thinking inout parameters must return a value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes