Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Functions
What will be the output of this Swift code?
func add(_ a: Int, _ b: Int) -> Int { return a + b }
let operation = add
print(operation(3, 4))
A34
Badd
C7
DError: Cannot assign function to variable
Step-by-Step Solution
Solution:
  1. Step 1: Assign function to variable

    The variable 'operation' stores the function 'add' without calling it.
  2. Step 2: Call the function via variable

    Calling 'operation(3, 4)' executes 'add(3, 4)' which returns 7.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Function call via variable = 7 [OK]
Quick Trick: Calling function variable runs original function [OK]
Common Mistakes:
  • Expecting variable to print function name
  • Thinking assignment causes error
  • Concatenating numbers as strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes