Bird
0
0

What is wrong with this Swift code?

medium📝 Debug Q7 of 15
Swift - Functions

What is wrong with this Swift code?

func doubleValue(_ num: inout Int) {
    num *= 2
}

let fixedNum = 8
doubleValue(&fixedNum)
AInout parameters cannot be used with integers
BMissing & before fixedNum
CFunction should return a value instead
DCannot pass a constant as an inout parameter
Step-by-Step Solution
Solution:
  1. Step 1: Identify the parameter type

    The function expects an inout Int, meaning it will modify the passed variable.
  2. Step 2: Check the argument passed

    fixedNum is declared as a constant with let, so it cannot be modified.
  3. Step 3: Understand Swift rules

    Only variables declared with var can be passed as inout parameters.
  4. Final Answer:

    Cannot pass a constant as an inout parameter -> Option D
  5. Quick Check:

    Constants cannot be modified [OK]
Quick Trick: Only vars can be passed as inout [OK]
Common Mistakes:
  • Passing constants instead of variables
  • Forgetting & when passing inout
  • Thinking inout works with let constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes