Bird
0
0

Why would you use an inout parameter in a Swift function instead of returning a new value?

easy📝 Conceptual Q1 of 15
Swift - Functions

Why would you use an inout parameter in a Swift function instead of returning a new value?

ATo declare a constant parameter that cannot be changed
BTo make the function run faster by avoiding copying
CTo ensure the parameter is immutable inside the function
DTo allow the function to modify the original variable directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand inout usage

    In Swift, inout parameters allow a function to modify the original variable passed in, rather than working on a copy.
  2. Step 2: Compare with returning values

    Returning a new value creates a copy and requires assignment outside the function, while inout modifies the original directly.
  3. Final Answer:

    To allow the function to modify the original variable directly -> Option D
  4. Quick Check:

    Modifies original variable directly [OK]
Quick Trick: Inout lets functions change original variables [OK]
Common Mistakes:
  • Thinking inout makes parameters immutable
  • Confusing inout with return values
  • Assuming inout improves performance by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes