Bird
0
0

Find the mistake in this Swift code:

medium📝 Debug Q7 of 15
Swift - Functions
Find the mistake in this Swift code:
func multiply(_ a: Int, _ b: Int) -> Int {
    return a * b
}
let operation: (Int, Int) -> Int = multiply
print(operation)
AMissing parentheses when calling operation
BPrinting 'operation' prints the function reference, not a result
CCannot assign multiply to operation variable
DFunction multiply has wrong return type
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable assignment

    'operation' correctly stores the function 'multiply'.
  2. Step 2: Calling vs printing function

    Printing 'operation' prints the function reference, not the result. To get output, call with parameters like 'operation(2,3)'.
  3. Final Answer:

    Missing parentheses when calling operation -> Option A
  4. Quick Check:

    Call function with () to get result = B [OK]
Quick Trick: Use parentheses to call function stored in variable [OK]
Common Mistakes:
  • Expecting function variable to print result without calling
  • Thinking function signature is incorrect
  • Assuming assignment is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes